直接上圖,接著上一篇的文本分割器,實驗要求如下:
1. 能進行文件分割
2. 分割塊大小由用戶輸入決定
3. 能進行文件合並
4. 文件分割與合並過程用線程來實現
5. 數據緩沖區不得超過2K
6. 要有處理進度顯示
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;
namespace WindowsFormsApplication1
{
public partial class 文件分割器1 : Form
{
//時間:2012-9-6
//作者:Olive
//功能:實現大文件的分割、合並
#region Members
int count1,count,fgkDaXiao;
FileInfo fFenGe,fHeBing;
FileStream fStream,hStream;
string fenGeLuJing,heBingLuJing;
int value = 0;
#endregion
#region Methods
public 文件分割器1()
{
InitializeComponent();
skinEngine1.SkinFile = "WarmColor1.ssk";
}
/// <summary>
/// 選擇要分割的文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnfgwenjian_Click(object sender, EventArgs e)
{
try
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fFenGe = new FileInfo(openFileDialog1.FileName);
txtfenge.Text = fFenGe.FullName;
fenGeLuJing = fFenGe.FullName.Substring(0, fFenGe.FullName.Length - openFileDialog1.SafeFileName.Length);
txtwjdaxiao.Text = ((int)fFenGe.Length/1024).ToString();
fStream = new FileStream(fFenGe.FullName, FileMode.Open, FileAccess.Read);
}
}
catch
{
MessageBox.Show("文件分割異常,請檢查確認無誤後再次分割!");
}
}
/// <summary>
/// 選擇要保存的路徑,如果不選默認為與原文件同路徑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnfglujing_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(txtfgkdaxiao.Text))
{
MessageBox.Show("請輸入文件分割塊大小!");
}
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
txtfglujing.Text= folderBrowserDialog1.SelectedPath;
fenGeLuJing = folderBrowserDialog1.SelectedPath+"\\";
}
}
/// <summary>
/// 分割文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnfenge_Click(object sender, EventArgs e)
{
try
{
int wenJianLength = Convert.ToInt32(txtwjdaxiao.Text);
if (wenJianLength % fgkDaXiao == 0)
{
count = wenJianLength / fgkDaXiao;
}
else
{
count = wenJianLength / fgkDaXiao + 1;
}
for (int i = 0; i < count; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(FenGe));
thread.Start(i);
}
MessageBox.Show("文件分割完畢");
}
catch
{
MessageBox.Show("分割異常,請確認操作!");
}
}
/// <summary>
/// 分割線程調用函數
/// </summary>
/// <param name="i"></param>
public void FenGe(object i)
{
try
{
using (FileStream fgstream = new FileStream(fenGeLuJing + fFenGe.Name.Substring(0, fFenGe.Name.Length - fFenGe.Extension.Length) + i + fFenGe.Extension, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[fgkDaXiao * 1024];
int data = 0;
if ((data = fStream.Read(buffer, 0, buffer.Length)) > 0)
{
BinaryWriter bWriter = new BinaryWriter(fgstream, Encoding.Default);
bWriter.Write(buffer, 0, data);
value=(int)i/count*100;
progressBar1.Value = value;
}
}
}
catch
{
MessageBox.Show("文件打開異常!");
}
}
/// <summary>
/// 輸入值變化
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txtfgkdaxiao_TextChanged(object sender, EventArgs e)
{
if (txtfgkdaxiao.Text.Length > 0)
{
fgkDaXiao = Convert.ToInt32(txtfgkdaxiao.Text);
}
}
/// <summary>
/// 選擇要合並的文件,首個分割文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnhbwenjian_Click(object sender, EventArgs e)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
fHeBing = new FileInfo(openFileDialog1.FileName);
txthebing.Text = fHeBing.FullName;
heBingLuJing = fHeBing.FullName.Substring(0, fHeBing.FullName.Length - openFileDialog1.SafeFileName.Length) + "\\";
}
}
/// <summary>
/// 選擇合並後的文件的保存路徑
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void btnhebinglujing_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
heBingLuJing = folderBrowserDialog1.SelectedPath + "\\";
}
}
/// <summary>
/// 合並文件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
if (count1 > 0)
{
using (hStream = new FileStream(heBingLuJing + fHeBing.Name.Substring(0, fHeBing.Name.Length - fHeBing.Extension.Length - 1) + fHeBing.Extension, FileMode.OpenOrCreate, FileAccess.Write))
{
for (int i = 0; i < count1; i++)
{
Thread thread = new Thread(new ParameterizedThreadStart(HeBing));
thread.Start(i);
}
MessageBox.Show("合並完成");
}
}
else
{
MessageBox.Show("請輸入合並文件數目");
}
}
catch
{
MessageBox.Show("合並異常,請重新合並");
}
}
/// <summary>
/// 求要合並的文件數
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void txthbkuaishu_TextChanged(object sender, EventArgs e)
{
count1 = Convert.ToInt32(txthbkuaishu.Text);
}
/// <summary>
/// 合並線程調用的合並函數,執行文件合並
/// </summary>
/// <param name="i"></param>
public void HeBing(object i)
{
using (FileStream readStream = new FileStream(fHeBing.FullName.Substring(0, fHeBing.FullName.Length - fHeBing.Extension.Length- 1) + i + fHeBing.Extension, FileMode.Open, FileAccess.Read))
{
byte[] buffer = new byte[readStream.Length];
int data = 0;
if ((data = readStream.Read(buffer, 0, buffer.Length)) > 0)
{
BinaryWriter binary = new BinaryWriter(hStream, Encoding.Default);
binary.Write(buffer, 0, data);
}
}
}
#endregion
}
}