C#實現(xiàn)文件分割和合并的示例詳解
更新時間:2022年12月26日 09:45:18 作者:芝麻粒兒
這篇文章主要為大家詳細(xì)介紹了如何利用C#實現(xiàn)文件分割和合并的功能,文中的示例代碼講解詳細(xì),對我們學(xué)習(xí)C#有一定的幫助,感興趣的小伙伴可以跟隨小編一起了解一下
實踐過程
效果
代碼
public partial class frmSplit : Form { public frmSplit() { InitializeComponent(); } #region 分割文件 /// <summary> /// 分割文件 /// </summary> /// <param name="strFlag">分割單位</param> /// <param name="intFlag">分割大小</param> /// <param name="strPath">分割后的文件存放路徑</param> /// <param name="strFile">要分割的文件</param> /// <param name="PBar">進(jìn)度條顯示</param> public void SplitFile(string strFlag, int intFlag, string strPath, string strFile, ProgressBar PBar) { int iFileSize = 0; //根據(jù)選擇來設(shè)定分割的小文件的大小 switch (strFlag) { case "Byte": iFileSize = intFlag; break; case "KB": iFileSize = intFlag * 1024; break; case "MB": iFileSize = intFlag * 1024 * 1024; break; case "GB": iFileSize = intFlag * 1024 * 1024 * 1024; break; } //以文件的全路徑對應(yīng)的字符串和文件打開模式來初始化FileStream文件流實例 FileStream SplitFileStream = new FileStream(strFile, FileMode.Open); //以FileStream文件流來初始化BinaryReader文件閱讀器 BinaryReader SplitFileReader = new BinaryReader(SplitFileStream); //每次分割讀取的最大數(shù)據(jù) byte[] TempBytes; //小文件總數(shù) int iFileCount = (int)(SplitFileStream.Length / iFileSize); PBar.Maximum = iFileCount; if (SplitFileStream.Length % iFileSize != 0) iFileCount++; string[] TempExtra = strFile.Split('.'); //循環(huán)將大文件分割成多個小文件 for (int i = 1; i <= iFileCount; i++) { //確定小文件的文件名稱 string sTempFileName = strPath + @"\" + i.ToString().PadLeft(4, '0') + "." + TempExtra[TempExtra.Length - 1]; //小文件名 //根據(jù)文件名稱和文件打開模式來初始化FileStream文件流實例 FileStream TempStream = new FileStream(sTempFileName, FileMode.OpenOrCreate); //以FileStream實例來創(chuàng)建、初始化BinaryWriter書寫器實例 BinaryWriter TempWriter = new BinaryWriter(TempStream); //從大文件中讀取指定大小數(shù)據(jù) TempBytes = SplitFileReader.ReadBytes(iFileSize); //把此數(shù)據(jù)寫入小文件 TempWriter.Write(TempBytes); //關(guān)閉書寫器,形成小文件 TempWriter.Close(); //關(guān)閉文件流 TempStream.Close(); PBar.Value = i - 1; } //關(guān)閉大文件閱讀器 SplitFileReader.Close(); SplitFileStream.Close(); MessageBox.Show("文件分割成功!"); } #endregion #region 合并文件 /// <summary> /// 合并文件 /// </summary> /// <param name="list">要合并的文件集合</param> /// <param name="strPath">合并后的文件名稱</param> /// <param name="PBar">進(jìn)度條顯示</param> public void CombinFile(string[] strFile, string strPath, ProgressBar PBar) { PBar.Maximum = strFile.Length; FileStream AddStream = null; //以合并后的文件名稱和打開方式來創(chuàng)建、初始化FileStream文件流 AddStream = new FileStream(strPath, FileMode.Append); //以FileStream文件流來初始化BinaryWriter書寫器,此用以合并分割的文件 BinaryWriter AddWriter = new BinaryWriter(AddStream); FileStream TempStream = null; BinaryReader TempReader = null; //循環(huán)合并小文件,并生成合并文件 for (int i = 0; i < strFile.Length; i++) { //以小文件所對應(yīng)的文件名稱和打開模式來初始化FileStream文件流,起讀取分割作用 TempStream = new FileStream(strFile[i].ToString(), FileMode.Open); TempReader = new BinaryReader(TempStream); //讀取分割文件中的數(shù)據(jù),并生成合并后文件 AddWriter.Write(TempReader.ReadBytes((int)TempStream.Length)); //關(guān)閉BinaryReader文件閱讀器 TempReader.Close(); //關(guān)閉FileStream文件流 TempStream.Close(); PBar.Value = i + 1; } //關(guān)閉BinaryWriter文件書寫器 AddWriter.Close(); //關(guān)閉FileStream文件流 AddStream.Close(); MessageBox.Show("文件合并成功!"); } #endregion private void frmSplit_Load(object sender, EventArgs e) { timer1.Start();//啟動計時器 } //選擇要分割的文件 private void btnSFile_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { txtFile.Text = openFileDialog.FileName; } } //執(zhí)行文件分割操作 private void btnSplit_Click(object sender, EventArgs e) { try { if (txtLength.Text == ""||txtFile.Text.Trim()==""||txtPath.Text.Trim()=="") { MessageBox.Show("請將信息填寫完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); txtLength.Focus(); } else if (cboxUnit.Text == "") { MessageBox.Show("請選擇要分割的文件單位!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); cboxUnit.Focus(); } else { SplitFile(cboxUnit.Text, Convert.ToInt32(txtLength.Text.Trim()), txtPath.Text, txtFile.Text, progressBar); } } catch { } } //選擇分割后的文件存放路徑 private void btnSPath_Click(object sender, EventArgs e) { if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { txtPath.Text = folderBrowserDialog.SelectedPath; } } //選擇要合成的文件 private void btnCFile_Click(object sender, EventArgs e) { if (openFileDialog.ShowDialog() == DialogResult.OK) { string Selectfile = ""; string[] files = openFileDialog.FileNames; for (int i = 0; i < files.Length; i++) { Selectfile += "," + files[i].ToString(); } if (Selectfile.StartsWith(",")) { Selectfile = Selectfile.Substring(1); } if (Selectfile.EndsWith(",")) { Selectfile.Remove(Selectfile.LastIndexOf(","),1); } txtCFile.Text = Selectfile; } } //選擇合成后的文件存放路徑 private void btnCPath_Click(object sender, EventArgs e) { if (saveFileDialog.ShowDialog() == DialogResult.OK) { txtCPath.Text = saveFileDialog.FileName; } } //執(zhí)行合成文件操作 private void btnCombin_Click(object sender, EventArgs e) { try { if (txtCFile.Text.Trim() == "" || txtCPath.Text.Trim() == "") { MessageBox.Show("請將信息輸入完整!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); } else { if (txtCFile.Text.IndexOf(",") == -1) MessageBox.Show("請選擇要合成的文件,最少為兩個!", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information); else { string[] strFiles = txtCFile.Text.Split(','); CombinFile(strFiles, txtCPath.Text, progressBar); } } } catch { } } //監(jiān)視“分割”/“合并”按鈕的可用狀態(tài) private void timer1_Tick(object sender, EventArgs e) { if (txtFile.Text != "" && txtPath.Text != "") btnSplit.Enabled = true; else btnSplit.Enabled = false; if (txtCFile.Text != "" && txtCPath.Text != "") btnCombin.Enabled = true; else btnCombin.Enabled = false; } }
partial class frmSplit { /// <summary> /// 必需的設(shè)計器變量。 /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// 清理所有正在使用的資源。 /// </summary> /// <param name="disposing">如果應(yīng)釋放托管資源,為 true;否則為 false。</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows 窗體設(shè)計器生成的代碼 /// <summary> /// 設(shè)計器支持所需的方法 - 不要 /// 使用代碼編輯器修改此方法的內(nèi)容。 /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.txtPath = new System.Windows.Forms.TextBox(); this.txtLength = new System.Windows.Forms.TextBox(); this.btnSPath = new System.Windows.Forms.Button(); this.label3 = new System.Windows.Forms.Label(); this.cboxUnit = new System.Windows.Forms.ComboBox(); this.label2 = new System.Windows.Forms.Label(); this.btnSplit = new System.Windows.Forms.Button(); this.btnSFile = new System.Windows.Forms.Button(); this.txtFile = new System.Windows.Forms.TextBox(); this.label1 = new System.Windows.Forms.Label(); this.tabPage2 = new System.Windows.Forms.TabPage(); this.txtCPath = new System.Windows.Forms.TextBox(); this.txtCFile = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.btnCPath = new System.Windows.Forms.Button(); this.btnCombin = new System.Windows.Forms.Button(); this.btnCFile = new System.Windows.Forms.Button(); this.label4 = new System.Windows.Forms.Label(); this.openFileDialog = new System.Windows.Forms.OpenFileDialog(); this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog(); this.timer1 = new System.Windows.Forms.Timer(this.components); this.saveFileDialog = new System.Windows.Forms.SaveFileDialog(); this.progressBar = new System.Windows.Forms.ProgressBar(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); this.tabPage2.SuspendLayout(); this.SuspendLayout(); // // tabControl1 // this.tabControl1.Controls.Add(this.tabPage1); this.tabControl1.Controls.Add(this.tabPage2); this.tabControl1.Dock = System.Windows.Forms.DockStyle.Fill; this.tabControl1.Location = new System.Drawing.Point(0, 0); this.tabControl1.Name = "tabControl1"; this.tabControl1.SelectedIndex = 0; this.tabControl1.Size = new System.Drawing.Size(354, 201); this.tabControl1.TabIndex = 0; // // tabPage1 // this.tabPage1.Controls.Add(this.txtPath); this.tabPage1.Controls.Add(this.txtLength); this.tabPage1.Controls.Add(this.btnSPath); this.tabPage1.Controls.Add(this.label3); this.tabPage1.Controls.Add(this.cboxUnit); this.tabPage1.Controls.Add(this.label2); this.tabPage1.Controls.Add(this.btnSplit); this.tabPage1.Controls.Add(this.btnSFile); this.tabPage1.Controls.Add(this.txtFile); this.tabPage1.Controls.Add(this.label1); this.tabPage1.Location = new System.Drawing.Point(4, 21); this.tabPage1.Name = "tabPage1"; this.tabPage1.Padding = new System.Windows.Forms.Padding(3); this.tabPage1.Size = new System.Drawing.Size(346, 176); this.tabPage1.TabIndex = 0; this.tabPage1.Text = "文件分割"; this.tabPage1.UseVisualStyleBackColor = true; // // txtPath // this.txtPath.Location = new System.Drawing.Point(37, 117); this.txtPath.Name = "txtPath"; this.txtPath.Size = new System.Drawing.Size(256, 21); this.txtPath.TabIndex = 10; // // txtLength // this.txtLength.Location = new System.Drawing.Point(37, 72); this.txtLength.Name = "txtLength"; this.txtLength.Size = new System.Drawing.Size(146, 21); this.txtLength.TabIndex = 9; // // btnSPath // this.btnSPath.Location = new System.Drawing.Point(299, 115); this.btnSPath.Name = "btnSPath"; this.btnSPath.Size = new System.Drawing.Size(37, 23); this.btnSPath.TabIndex = 8; this.btnSPath.Text = "<<"; this.btnSPath.UseVisualStyleBackColor = true; this.btnSPath.Click += new System.EventHandler(this.btnSPath_Click); // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(12, 100); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(137, 12); this.label3.TabIndex = 7; this.label3.Text = "選擇分割后文件存放路徑"; // // cboxUnit // this.cboxUnit.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; this.cboxUnit.FormattingEnabled = true; this.cboxUnit.Items.AddRange(new object[] { "Byte", "KB", "MB", "GB"}); this.cboxUnit.Location = new System.Drawing.Point(189, 73); this.cboxUnit.Name = "cboxUnit"; this.cboxUnit.Size = new System.Drawing.Size(63, 20); this.cboxUnit.TabIndex = 6; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(12, 55); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(101, 12); this.label2.TabIndex = 5; this.label2.Text = "設(shè)置分割文件大小"; // // btnSplit // this.btnSplit.Location = new System.Drawing.Point(261, 26); this.btnSplit.Name = "btnSplit"; this.btnSplit.Size = new System.Drawing.Size(75, 23); this.btnSplit.TabIndex = 3; this.btnSplit.Text = "分割"; this.btnSplit.UseVisualStyleBackColor = true; this.btnSplit.Click += new System.EventHandler(this.btnSplit_Click); // // btnSFile // this.btnSFile.Location = new System.Drawing.Point(220, 26); this.btnSFile.Name = "btnSFile"; this.btnSFile.Size = new System.Drawing.Size(40, 23); this.btnSFile.TabIndex = 2; this.btnSFile.Text = "<<"; this.btnSFile.UseVisualStyleBackColor = true; this.btnSFile.Click += new System.EventHandler(this.btnSFile_Click); // // txtFile // this.txtFile.Location = new System.Drawing.Point(37, 27); this.txtFile.Name = "txtFile"; this.txtFile.Size = new System.Drawing.Size(179, 21); this.txtFile.TabIndex = 1; // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(12, 10); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(113, 12); this.label1.TabIndex = 0; this.label1.Text = "請選擇要分割的文件"; // // tabPage2 // this.tabPage2.Controls.Add(this.txtCPath); this.tabPage2.Controls.Add(this.txtCFile); this.tabPage2.Controls.Add(this.label5); this.tabPage2.Controls.Add(this.btnCPath); this.tabPage2.Controls.Add(this.btnCombin); this.tabPage2.Controls.Add(this.btnCFile); this.tabPage2.Controls.Add(this.label4); this.tabPage2.Location = new System.Drawing.Point(4, 21); this.tabPage2.Name = "tabPage2"; this.tabPage2.Padding = new System.Windows.Forms.Padding(3); this.tabPage2.Size = new System.Drawing.Size(346, 176); this.tabPage2.TabIndex = 1; this.tabPage2.Text = "文件合成"; this.tabPage2.UseVisualStyleBackColor = true; // // txtCPath // this.txtCPath.Location = new System.Drawing.Point(37, 88); this.txtCPath.Name = "txtCPath"; this.txtCPath.Size = new System.Drawing.Size(256, 21); this.txtCPath.TabIndex = 6; // // txtCFile // this.txtCFile.Location = new System.Drawing.Point(37, 44); this.txtCFile.Name = "txtCFile"; this.txtCFile.Size = new System.Drawing.Size(174, 21); this.txtCFile.TabIndex = 5; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(11, 72); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(173, 12); this.label5.TabIndex = 4; this.label5.Text = "選擇合并后文件存放路徑及名稱"; // // btnCPath // this.btnCPath.Location = new System.Drawing.Point(299, 87); this.btnCPath.Name = "btnCPath"; this.btnCPath.Size = new System.Drawing.Size(38, 23); this.btnCPath.TabIndex = 3; this.btnCPath.Text = "<<"; this.btnCPath.UseVisualStyleBackColor = true; this.btnCPath.Click += new System.EventHandler(this.btnCPath_Click); // // btnCombin // this.btnCombin.Location = new System.Drawing.Point(262, 44); this.btnCombin.Name = "btnCombin"; this.btnCombin.Size = new System.Drawing.Size(75, 23); this.btnCombin.TabIndex = 2; this.btnCombin.Text = "合并"; this.btnCombin.UseVisualStyleBackColor = true; this.btnCombin.Click += new System.EventHandler(this.btnCombin_Click); // // btnCFile // this.btnCFile.Location = new System.Drawing.Point(217, 44); this.btnCFile.Name = "btnCFile"; this.btnCFile.Size = new System.Drawing.Size(39, 23); this.btnCFile.TabIndex = 1; this.btnCFile.Text = "<<"; this.btnCFile.UseVisualStyleBackColor = true; this.btnCFile.Click += new System.EventHandler(this.btnCFile_Click); // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(11, 28); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(101, 12); this.label4.TabIndex = 0; this.label4.Text = "選擇要合成的文件"; // // openFileDialog // this.openFileDialog.Multiselect = true; // // timer1 // this.timer1.Tick += new System.EventHandler(this.timer1_Tick); // // progressBar // this.progressBar.Dock = System.Windows.Forms.DockStyle.Bottom; this.progressBar.Location = new System.Drawing.Point(0, 181); this.progressBar.Name = "progressBar"; this.progressBar.Size = new System.Drawing.Size(354, 20); this.progressBar.TabIndex = 1; // // frmSplit // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(354, 201); this.Controls.Add(this.progressBar); this.Controls.Add(this.tabControl1); this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "frmSplit"; this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen; this.Text = "文件分割與合成"; this.Load += new System.EventHandler(this.frmSplit_Load); this.tabControl1.ResumeLayout(false); this.tabPage1.ResumeLayout(false); this.tabPage1.PerformLayout(); this.tabPage2.ResumeLayout(false); this.tabPage2.PerformLayout(); this.ResumeLayout(false); } #endregion public System.Windows.Forms.TabControl tabControl1; private System.Windows.Forms.TabPage tabPage1; private System.Windows.Forms.TextBox txtFile; private System.Windows.Forms.Label label1; private System.Windows.Forms.TabPage tabPage2; private System.Windows.Forms.TextBox txtPath; private System.Windows.Forms.TextBox txtLength; private System.Windows.Forms.Button btnSPath; private System.Windows.Forms.Label label3; private System.Windows.Forms.ComboBox cboxUnit; private System.Windows.Forms.Label label2; private System.Windows.Forms.Button btnSplit; private System.Windows.Forms.Button btnSFile; private System.Windows.Forms.OpenFileDialog openFileDialog; private System.Windows.Forms.FolderBrowserDialog folderBrowserDialog; private System.Windows.Forms.TextBox txtCPath; private System.Windows.Forms.TextBox txtCFile; private System.Windows.Forms.Label label5; private System.Windows.Forms.Button btnCPath; private System.Windows.Forms.Button btnCombin; private System.Windows.Forms.Button btnCFile; private System.Windows.Forms.Label label4; private System.Windows.Forms.Timer timer1; private System.Windows.Forms.SaveFileDialog saveFileDialog; private System.Windows.Forms.ProgressBar progressBar; }
到此這篇關(guān)于C#實現(xiàn)文件分割和合并的示例詳解的文章就介紹到這了,更多相關(guān)C#文件分割 合并內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
分享C#操作內(nèi)存讀寫方法的主要實現(xiàn)代碼
這篇文章介紹了C#操作內(nèi)存讀寫方法的主要實現(xiàn)代碼,下面讓我們來看看具體的實例實現(xiàn),有需要的朋友可以參考一下2013-08-08c#不使用系統(tǒng)api實現(xiàn)可以指定區(qū)域屏幕截屏功能
這篇文章主要介紹了不使用系統(tǒng)API通過純c#實現(xiàn)屏幕指定區(qū)域截屏功能,截屏后還可以保存圖象文件,大家參考使用吧2014-01-01C#微信公眾平臺開發(fā)之a(chǎn)ccess_token的獲取存儲與更新
這篇文章主要介紹了C#微信公眾平臺開發(fā)之a(chǎn)ccess_token的獲取存儲與更新的相關(guān)資料,需要的朋友可以參考下2016-03-03C#中JavaScriptSerializer幫助類用法實例
這篇文章主要介紹了C#中JavaScriptSerializer幫助類用法,實例分析了JavaScriptSerializer幫助類處理json字符串時的技巧,需要的朋友可以參考下2014-12-12C#實現(xiàn)windows form拷貝內(nèi)容到剪貼板的方法
這篇文章主要介紹了C#實現(xiàn)windows form拷貝內(nèi)容到剪貼板的方法,涉及C#操作Clipboard的相關(guān)技巧,需要的朋友可以參考下2015-06-06