您的位置:首页 > 其它

文件分割合并功能

2012-03-22 16:56 387 查看
文件分割合并功能
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

//选择文件 事件
private void Btn_SelFile_Click(object sender, EventArgs e)
{
string filter="选择文件(*.*)|*.*";
OpenFileSelection ofs = new OpenFileSelection(filter,false);
if(ofs.IsFileSelected()==true)
{
this.textBox1.Text =ofs.GetFileName;
}
}

//选择目录 事件
private void btn_seldir_Click(object sender, EventArgs e)
{
DirectorySelection dirsel = new DirectorySelection(Environment.SpecialFolder.Desktop, "", true);
if (dirsel.IsDirectorySelected() == true)
{
this.textBox2.Text = dirsel.GetSelectedPath;
}
}

//开始分割
private void Btn_Split_Click(object sender, EventArgs e)
{
try
{
string fpath = this.textBox1.Text;
string splitDir = this.textBox2.Text;

SplitFile(fpath, splitDir, 10);
MessageBox.Show("分割完毕!", "提示");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "提示");
}
}

public  bool SplitFile(string sourefilepath, string splitedStoreDir, int fileNums)
{
bool rbc = false;
FileStream fs = new FileStream(sourefilepath, FileMode.Open);
long fileLength = fs.Length;
long fNum = (long)fileNums;
long filesize = fileLength/fNum;
for (int i = 0; i < fNum; i++)
{
int tmpfsize = (int)filesize;
string tmpfName = (i + 1).ToString();
string tmpfpath=splitedStoreDir + "\\" + tmpfName;
FileStream wfs = new FileStream(tmpfpath,FileMode.OpenOrCreate);
byte[] array = null;
if (i == fNum - 1)
{
long len_pos=fs.Length-fs.Position;
array = new byte[len_pos];
fs.Read(array, 0, (int)len_pos);
wfs.Write(array, 0, (int)len_pos);
}
else
{
array = new byte[tmpfsize];
fs.Read(array, 0, tmpfsize);
wfs.Write(array, 0, tmpfsize);
}
wfs.Flush();
wfs.Close();
wfs.Dispose();
wfs = null;
}
fs.Close();
fs.Dispose();
fs = null;

return rbc;
}

public bool UnionFile(string splitedStoreDir, string unionfilepath)
{
bool rbc = false;
string[] filepathArray = CommonClass.GetFiles(splitedStoreDir, false);
List<int> list = new List<int>();
foreach (string t in filepathArray) list.Add(CommonClass.TInt(t.Replace("\\","")));
list.Sort();
FileStream wfs = new FileStream(unionfilepath, FileMode.Append);
for (int i = 0; i < list.Count; i++)
{
string fp = splitedStoreDir+"\\"+list[i].ToString();
FileStream fs = new FileStream(fp, FileMode.Open);
byte[] dataArray=new byte[fs.Length];
fs.Read(dataArray,0,(int)fs.Length);
wfs.Write(dataArray,0, (int)fs.Length);
//
fs.Close();
fs.Dispose();
fs = null;
}
wfs.Flush();
wfs.Close();
wfs.Dispose();
wfs = null;
return rbc;
}
//开始合并
private void btn_union_Click(object sender, EventArgs e)
{
try
{
string splitDir = this.textBox2.Text;

UnionFile(splitDir, splitDir + "\\union.iso");
MessageBox.Show("合并完毕!", "提示");
}
catch (Exception ee)
{
MessageBox.Show(ee.Message, "提示");
}
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: