using System;
using System.IO;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace File_Merger_and_Splitter
{
	/// <summary>
	/// MainApp 的摘要说明。
	/// </summary>
	public class MainApp : System.Windows.Forms.Form
	{
		private System.Windows.Forms.TabControl tabControl1;
		private System.Windows.Forms.TabPage tpSplit;
		private System.Windows.Forms.TabPage tpMerge;
		private System.Windows.Forms.Label label1;
		private System.Windows.Forms.Button btnSplit;
		private System.Windows.Forms.Button btnSplitBrowse;
		private System.Windows.Forms.Label label2;
		private System.Windows.Forms.Button btnMergeBrowse;
		private System.Windows.Forms.Button btnMerge;
		private System.Windows.Forms.TextBox txtFileNameToSplit;
		private System.Windows.Forms.TextBox txtFileNameToMerge;
		private System.Windows.Forms.OpenFileDialog openFileDialog;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.Label label4;
		private System.Windows.Forms.Label label5;
		private System.Windows.Forms.TextBox txtFragmentSize;

		private FileInfo f = null; 
		private int filesize=0;
		private int fragmentsize=1440000;
		private System.Windows.Forms.Label label6;
		private System.Windows.Forms.TextBox txtFileSize;
		private System.Windows.Forms.Label label7;
		private System.Windows.Forms.Label label8;
		private System.Windows.Forms.Label label9;
		private System.Windows.Forms.StatusBar statusBarSplit;
		private System.Windows.Forms.TextBox txtFileNameMergeTo;
		private System.Windows.Forms.StatusBar statusBarMerge;
		private System.Windows.Forms.TextBox txtSplitFragmentCount;
		private System.Windows.Forms.TextBox txtMergeFragmentCount;
		/// <summary>
		/// 必需的设计器变量。
		/// </summary>
		private System.ComponentModel.Container components = null;

		public MainApp()
		{
			//
			// Windows 窗体设计器支持所必需的
			//
			InitializeComponent();

			//
			// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
			//
		}

		/// <summary>
		/// 清理所有正在使用的资源。
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// 设计器支持所需的方法 - 不要使用代码编辑器修改
		/// 此方法的内容。
		/// </summary>
		private void InitializeComponent()
		{
			this.tabControl1 = new System.Windows.Forms.TabControl();
			this.tpSplit = new System.Windows.Forms.TabPage();
			this.statusBarSplit = new System.Windows.Forms.StatusBar();
			this.label7 = new System.Windows.Forms.Label();
			this.txtFileSize = new System.Windows.Forms.TextBox();
			this.label6 = new System.Windows.Forms.Label();
			this.txtSplitFragmentCount = new System.Windows.Forms.TextBox();
			this.label5 = new System.Windows.Forms.Label();
			this.label4 = new System.Windows.Forms.Label();
			this.txtFragmentSize = new System.Windows.Forms.TextBox();
			this.label3 = new System.Windows.Forms.Label();
			this.btnSplit = new System.Windows.Forms.Button();
			this.btnSplitBrowse = new System.Windows.Forms.Button();
			this.txtFileNameToSplit = new System.Windows.Forms.TextBox();
			this.label1 = new System.Windows.Forms.Label();
			this.tpMerge = new System.Windows.Forms.TabPage();
			this.btnMerge = new System.Windows.Forms.Button();
			this.btnMergeBrowse = new System.Windows.Forms.Button();
			this.txtFileNameToMerge = new System.Windows.Forms.TextBox();
			this.label2 = new System.Windows.Forms.Label();
			this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
			this.label8 = new System.Windows.Forms.Label();
			this.label9 = new System.Windows.Forms.Label();
			this.txtMergeFragmentCount = new System.Windows.Forms.TextBox();
			this.txtFileNameMergeTo = new System.Windows.Forms.TextBox();
			this.statusBarMerge = new System.Windows.Forms.StatusBar();
			this.tabControl1.SuspendLayout();
			this.tpSplit.SuspendLayout();
			this.tpMerge.SuspendLayout();
			this.SuspendLayout();
			// 
			// tabControl1
			// 
			this.tabControl1.Controls.AddRange(new System.Windows.Forms.Control[] {
																					  this.tpSplit,
																					  this.tpMerge});
			this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill;
			this.tabControl1.Name = "tabControl1";
			this.tabControl1.SelectedIndex = 0;
			this.tabControl1.Size = new System.Drawing.Size(328, 237);
			this.tabControl1.TabIndex = 0;
			// 
			// tpSplit
			// 
			this.tpSplit.Controls.AddRange(new System.Windows.Forms.Control[] {
																				  this.statusBarSplit,
																				  this.label7,
																				  this.txtFileSize,
																				  this.label6,
																				  this.txtSplitFragmentCount,
																				  this.label5,
																				  this.label4,
																				  this.txtFragmentSize,
																				  this.label3,
																				  this.btnSplit,
																				  this.btnSplitBrowse,
																				  this.txtFileNameToSplit,
																				  this.label1});
			this.tpSplit.Location = new System.Drawing.Point(4, 21);
			this.tpSplit.Name = "tpSplit";
			this.tpSplit.Size = new System.Drawing.Size(320, 212);
			this.tpSplit.TabIndex = 0;
			this.tpSplit.Text = "拆分";
			// 
			// statusBarSplit
			// 
			this.statusBarSplit.Location = new System.Drawing.Point(0, 190);
			this.statusBarSplit.Name = "statusBarSplit";
			this.statusBarSplit.Size = new System.Drawing.Size(320, 22);
			this.statusBarSplit.TabIndex = 12;
			// 
			// label7
			// 
			this.label7.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label7.Location = new System.Drawing.Point(256, 64);
			this.label7.Name = "label7";
			this.label7.Size = new System.Drawing.Size(48, 16);
			this.label7.TabIndex = 11;
			this.label7.Text = "字节";
			// 
			// txtFileSize
			// 
			this.txtFileSize.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtFileSize.Location = new System.Drawing.Point(80, 56);
			this.txtFileSize.Name = "txtFileSize";
			this.txtFileSize.Size = new System.Drawing.Size(160, 22);
			this.txtFileSize.TabIndex = 10;
			this.txtFileSize.Text = "";
			// 
			// label6
			// 
			this.label6.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label6.Location = new System.Drawing.Point(8, 56);
			this.label6.Name = "label6";
			this.label6.Size = new System.Drawing.Size(64, 23);
			this.label6.TabIndex = 9;
			this.label6.Text = "文件长度";
			// 
			// txtSplitFragmentCount
			// 
			this.txtSplitFragmentCount.Enabled = false;
			this.txtSplitFragmentCount.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtSplitFragmentCount.Location = new System.Drawing.Point(80, 120);
			this.txtSplitFragmentCount.Name = "txtSplitFragmentCount";
			this.txtSplitFragmentCount.Size = new System.Drawing.Size(160, 22);
			this.txtSplitFragmentCount.TabIndex = 8;
			this.txtSplitFragmentCount.Text = "";
			// 
			// label5
			// 
			this.label5.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label5.Location = new System.Drawing.Point(8, 120);
			this.label5.Name = "label5";
			this.label5.Size = new System.Drawing.Size(56, 23);
			this.label5.TabIndex = 7;
			this.label5.Text = "分块数";
			// 
			// label4
			// 
			this.label4.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label4.Location = new System.Drawing.Point(256, 96);
			this.label4.Name = "label4";
			this.label4.Size = new System.Drawing.Size(48, 16);
			this.label4.TabIndex = 6;
			this.label4.Text = "字节";
			// 
			// txtFragmentSize
			// 
			this.txtFragmentSize.Enabled = false;
			this.txtFragmentSize.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtFragmentSize.Location = new System.Drawing.Point(80, 88);
			this.txtFragmentSize.Name = "txtFragmentSize";
			this.txtFragmentSize.Size = new System.Drawing.Size(160, 22);
			this.txtFragmentSize.TabIndex = 5;
			this.txtFragmentSize.Text = "1440000";
			this.txtFragmentSize.TextChanged += new System.EventHandler(this.txtFragmentSize_TextChanged);
			// 
			// label3
			// 
			this.label3.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label3.Location = new System.Drawing.Point(8, 88);
			this.label3.Name = "label3";
			this.label3.Size = new System.Drawing.Size(64, 16);
			this.label3.TabIndex = 4;
			this.label3.Text = "分块长度";
			// 
			// btnSplit
			// 
			this.btnSplit.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnSplit.Location = new System.Drawing.Point(136, 160);
			this.btnSplit.Name = "btnSplit";
			this.btnSplit.Size = new System.Drawing.Size(72, 24);
			this.btnSplit.TabIndex = 3;
			this.btnSplit.Text = "拆分";
			this.btnSplit.Click += new System.EventHandler(this.btnSplit_Click);
			// 
			// btnSplitBrowse
			// 
			this.btnSplitBrowse.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnSplitBrowse.Location = new System.Drawing.Point(256, 24);
			this.btnSplitBrowse.Name = "btnSplitBrowse";
			this.btnSplitBrowse.Size = new System.Drawing.Size(48, 24);
			this.btnSplitBrowse.TabIndex = 2;
			this.btnSplitBrowse.Text = "浏览";
			this.btnSplitBrowse.Click += new System.EventHandler(this.btnSplitBrowse_Click);
			// 
			// txtFileNameToSplit
			// 
			this.txtFileNameToSplit.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtFileNameToSplit.Location = new System.Drawing.Point(80, 24);
			this.txtFileNameToSplit.Name = "txtFileNameToSplit";
			this.txtFileNameToSplit.Size = new System.Drawing.Size(160, 22);
			this.txtFileNameToSplit.TabIndex = 1;
			this.txtFileNameToSplit.Text = "";
			this.txtFileNameToSplit.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.txtFileNameToSplit_KeyPress);
			this.txtFileNameToSplit.TextChanged += new System.EventHandler(this.txtFileNameToSplit_TextChanged);
			// 
			// label1
			// 
			this.label1.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label1.Location = new System.Drawing.Point(8, 24);
			this.label1.Name = "label1";
			this.label1.Size = new System.Drawing.Size(40, 23);
			this.label1.TabIndex = 0;
			this.label1.Text = "文件";
			// 
			// tpMerge
			// 
			this.tpMerge.Controls.AddRange(new System.Windows.Forms.Control[] {
																				  this.statusBarMerge,
																				  this.txtFileNameMergeTo,
																				  this.txtMergeFragmentCount,
																				  this.label9,
																				  this.label8,
																				  this.btnMerge,
																				  this.btnMergeBrowse,
																				  this.txtFileNameToMerge,
																				  this.label2});
			this.tpMerge.Location = new System.Drawing.Point(4, 21);
			this.tpMerge.Name = "tpMerge";
			this.tpMerge.Size = new System.Drawing.Size(320, 212);
			this.tpMerge.TabIndex = 1;
			this.tpMerge.Text = "组装";
			// 
			// btnMerge
			// 
			this.btnMerge.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnMerge.Location = new System.Drawing.Point(136, 160);
			this.btnMerge.Name = "btnMerge";
			this.btnMerge.Size = new System.Drawing.Size(72, 24);
			this.btnMerge.TabIndex = 3;
			this.btnMerge.Text = "组装";
			this.btnMerge.Click += new System.EventHandler(this.btnMerge_Click);
			// 
			// btnMergeBrowse
			// 
			this.btnMergeBrowse.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.btnMergeBrowse.Location = new System.Drawing.Point(256, 24);
			this.btnMergeBrowse.Name = "btnMergeBrowse";
			this.btnMergeBrowse.Size = new System.Drawing.Size(48, 24);
			this.btnMergeBrowse.TabIndex = 2;
			this.btnMergeBrowse.Text = "浏览";
			this.btnMergeBrowse.Click += new System.EventHandler(this.btnMergeBrowse_Click);
			// 
			// txtFileNameToMerge
			// 
			this.txtFileNameToMerge.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtFileNameToMerge.Location = new System.Drawing.Point(80, 24);
			this.txtFileNameToMerge.Name = "txtFileNameToMerge";
			this.txtFileNameToMerge.Size = new System.Drawing.Size(160, 22);
			this.txtFileNameToMerge.TabIndex = 1;
			this.txtFileNameToMerge.Text = "";
			// 
			// label2
			// 
			this.label2.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label2.Location = new System.Drawing.Point(8, 24);
			this.label2.Name = "label2";
			this.label2.Size = new System.Drawing.Size(64, 23);
			this.label2.TabIndex = 0;
			this.label2.Text = "第一分块";
			// 
			// label8
			// 
			this.label8.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label8.Location = new System.Drawing.Point(8, 56);
			this.label8.Name = "label8";
			this.label8.Size = new System.Drawing.Size(64, 23);
			this.label8.TabIndex = 4;
			this.label8.Text = "分块数目";
			// 
			// label9
			// 
			this.label9.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.label9.Location = new System.Drawing.Point(8, 88);
			this.label9.Name = "label9";
			this.label9.Size = new System.Drawing.Size(64, 23);
			this.label9.TabIndex = 5;
			this.label9.Text = "组装文件";
			// 
			// txtMergeFragmentCount
			// 
			this.txtMergeFragmentCount.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtMergeFragmentCount.Location = new System.Drawing.Point(80, 56);
			this.txtMergeFragmentCount.Name = "txtMergeFragmentCount";
			this.txtMergeFragmentCount.Size = new System.Drawing.Size(160, 22);
			this.txtMergeFragmentCount.TabIndex = 7;
			this.txtMergeFragmentCount.Text = "";
			// 
			// txtFileNameMergeTo
			// 
			this.txtFileNameMergeTo.Font = new System.Drawing.Font("Tahoma", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
			this.txtFileNameMergeTo.Location = new System.Drawing.Point(80, 88);
			this.txtFileNameMergeTo.Name = "txtFileNameMergeTo";
			this.txtFileNameMergeTo.Size = new System.Drawing.Size(160, 22);
			this.txtFileNameMergeTo.TabIndex = 8;
			this.txtFileNameMergeTo.Text = "";
			// 
			// statusBarMerge
			// 
			this.statusBarMerge.Location = new System.Drawing.Point(0, 188);
			this.statusBarMerge.Name = "statusBarMerge";
			this.statusBarMerge.Size = new System.Drawing.Size(320, 24);
			this.statusBarMerge.TabIndex = 11;
			// 
			// MainApp
			// 
			this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
			this.ClientSize = new System.Drawing.Size(328, 237);
			this.Controls.AddRange(new System.Windows.Forms.Control[] {
																		  this.tabControl1});
			this.Name = "MainApp";
			this.Text = "文件分块器";
			this.tabControl1.ResumeLayout(false);
			this.tpSplit.ResumeLayout(false);
			this.tpMerge.ResumeLayout(false);
			this.ResumeLayout(false);

		}
		#endregion

		public static void Main(string[] args) 
		{
			Application.Run(new MainApp());
		}

		private void btnSplitBrowse_Click(object sender, System.EventArgs e)
		{
			openFileDialog.ShowDialog();
			txtFileNameToSplit.Text=openFileDialog.FileName;
			DisplayFileSize();
		}

		private void txtFileNameToSplit_TextChanged(object sender, System.EventArgs e)
		{
			if (txtFileNameToSplit.Text!="")
			{
				txtFragmentSize.Enabled =true;
				txtSplitFragmentCount.Enabled=true;
			}
		}

		private void txtFileNameToSplit_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
		{
			if(e.KeyChar == '\r')
				DisplayFileSize();
		}

		private void DisplayFileSize()
		{
			if (this.tabControl1.SelectedIndex==0)
			{
				f = new FileInfo(txtFileNameToSplit.Text);
				filesize=(int)f.Length;
				txtFileSize.Text=filesize.ToString();
				txtSplitFragmentCount.Text=System.Math.Ceiling(Double.Parse(txtFileSize.Text)/Double.Parse(txtFragmentSize.Text)).ToString();
			}
			else
			{
				string targetfilename=txtFileNameToMerge.Text.Substring(0,txtFileNameToMerge.Text.Length-5);
				int i=0;
				while (File.Exists(targetfilename+i+".fsm"))
				{
					i++;
				}
				txtMergeFragmentCount.Text=i.ToString();
				txtFileNameMergeTo.Text=targetfilename;
			}
		}

		private void btnSplit_Click(object sender, System.EventArgs e)
		{
			FileStream inFile = new FileStream(f.ToString(),FileMode.OpenOrCreate, FileAccess.Read);
			for (int i = 0; i<Int32.Parse(txtSplitFragmentCount.Text) ; i++)
			{
				FileStream outFile = new FileStream(f.ToString()+i+".fsm",FileMode.OpenOrCreate, FileAccess.Write);
				int data=0;

				byte [] buffer = new byte [fragmentsize];
				if ((data=inFile.Read(buffer,0,fragmentsize))>0)
				{
					outFile.Write(buffer,0,data);
				}
				outFile.Close();
				statusBarSplit.Text=100*i/Int32.Parse(txtSplitFragmentCount.Text)+"%";
			}
			inFile.Close();			
			statusBarSplit.Text = "文件拆分成功完成！";
		}

		private void txtFragmentSize_TextChanged(object sender, System.EventArgs e)
		{
			fragmentsize=int.Parse(txtFragmentSize.Text);
		}

		private void btnMerge_Click(object sender, System.EventArgs e)
		{
			
			FileStream outFile = new FileStream(txtFileNameMergeTo.Text,FileMode.OpenOrCreate, FileAccess.Write);

			for (int i=0;i<Int32.Parse(txtMergeFragmentCount.Text); i++)
			{
				int data=0;
				byte [] buffer = new byte [1024];
				FileStream inFile = new FileStream(txtFileNameMergeTo.Text +i+".fsm",FileMode.OpenOrCreate, FileAccess.Read);
				while ((data=inFile.Read(buffer,0,1024))>0)
				{
					outFile.Write(buffer,0,data);
				}
				inFile.Close();
				statusBarMerge.Text=100*i/Int32.Parse(txtMergeFragmentCount.Text)+"%";
			}
			outFile.Close();
			statusBarMerge.Text = "文件组装成功完成！";
		}

		private void btnMergeBrowse_Click(object sender, System.EventArgs e)
		{
			openFileDialog.ShowDialog();
			txtFileNameToMerge.Text=openFileDialog.FileName;
			DisplayFileSize();
        }


	}

		
}
