C# 文件拆分器
2024-07-21 02:19:00
供稿:网友
本文来源于网页设计爱好者web开发社区http://www.html.org.cn收集整理,欢迎访问。组合时采用了两层的copy命令,可多组合一些文件,其实用测试命令行总长度的办法可以理论上实现无限拆分文件的组合,但实用价值就不高了,拆成万余份文件不但此单线程方法显得效率低下,而且应当用更优秀算法进行分割和组合。
这个程序最终只能归为“玩具”一类。
using system;
using system.drawing;
using system.collections;
using system.componentmodel;
using system.windows.forms;
using system.data;
namespace filesplit
{
/// <summary>
/// form1 的摘要说明。
/// </summary>
public class form1 : system.windows.forms.form
{
private system.windows.forms.textbox textbox1;
private system.windows.forms.textbox textbox2;
private system.windows.forms.button button1;
private system.windows.forms.button button2;
private system.windows.forms.openfiledialog ofd;
private system.windows.forms.label label1;
private system.windows.forms.label label2;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private system.componentmodel.container components = null;
public form1()
{
//
// windows 窗体设计器支持所必需的
//
initializecomponent();
//
// todo: 在 initializecomponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.dispose();
}
}
base.dispose( disposing );
}
#region windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void initializecomponent()
{
this.textbox1 = new system.windows.forms.textbox();
this.textbox2 = new system.windows.forms.textbox();
this.button1 = new system.windows.forms.button();
this.button2 = new system.windows.forms.button();
this.ofd = new system.windows.forms.openfiledialog();
this.label1 = new system.windows.forms.label();
this.label2 = new system.windows.forms.label();
this.suspendlayout();
//
// textbox1
//
this.textbox1.borderstyle = system.windows.forms.borderstyle.fixedsingle;
this.textbox1.location = new system.drawing.point(16, 16);
this.textbox1.name = "textbox1";
this.textbox1.readonly = true;
this.textbox1.size = new system.drawing.size(376, 21);
this.textbox1.tabindex = 0;
this.textbox1.text = "file path";
//
// textbox2
//
this.textbox2.location = new system.drawing.point(56, 48);
this.textbox2.name = "textbox2";
this.textbox2.size = new system.drawing.size(72, 21);
this.textbox2.tabindex = 1;
this.textbox2.text = "";
//
// button1
//
this.button1.flatstyle = system.windows.forms.flatstyle.system;
this.button1.location = new system.drawing.point(200, 48);
this.button1.name = "button1";
this.button1.size = new system.drawing.size(272, 24);
this.button1.tabindex = 2;
this.button1.text = "拆分";
this.button1.click += new system.eventhandler(this.button1_click);
//
// button2
//
this.button2.flatstyle = system.windows.forms.flatstyle.system;
this.button2.location = new system.drawing.point(400, 16);
this.button2.name = "button2";
this.button2.size = new system.drawing.size(72, 24);
this.button2.tabindex = 3;
this.button2.text = "浏览";
this.button2.click += new system.eventhandler(this.button2_click);
//
// label1
//
this.label1.location = new system.drawing.point(16, 48);
this.label1.name = "label1";
this.label1.size = new system.drawing.size(32, 23);
this.label1.tabindex = 4;
this.label1.text = "尺寸";
//
// label2
//
this.label2.location = new system.drawing.point(136, 48);
this.label2.name = "label2";
this.label2.size = new system.drawing.size(56, 16);
this.label2.tabindex = 5;
this.label2.text = "kb";
//
// form1
//
this.autoscalebasesize = new system.drawing.size(6, 14);
this.clientsize = new system.drawing.size(498, 79);
this.controls.add(this.label2);
this.controls.add(this.label1);
this.controls.add(this.button2);
this.controls.add(this.button1);
this.controls.add(this.textbox2);
this.controls.add(this.textbox1);
this.formborderstyle = system.windows.forms.formborderstyle.fixedsingle;
this.maximizebox = false;
this.minimizebox = false;
this.name = "form1";
this.text = "spliter";
this.resumelayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[stathread]
static void main()
{
application.run(new form1());
}
private void button2_click(object sender, system.eventargs e)
{
if(this.ofd.showdialog()!=system.windows.forms.dialogresult.ok){return;}
this.textbox1.text=ofd.filename;
}
private void button1_click(object sender, system.eventargs e)
{
try
{
int i=1;
try
{
i=int.parse(this.textbox2.text);
if (i <= 0) { system.exception ee = new exception(); throw ee; }
i*=1000;
}
catch
{
i=0;
system.windows.forms.messagebox.show("输入拆分数字不正确");
return;
}
#region "分配文件夹"
int count=1;
string workpath=ofd.filename.tostring()+" split";
while(true)
{
if(!system.io.directory.exists(workpath)){break;}
else
{
if(!system.io.directory.exists(workpath+"_"+count.tostring()))
{
workpath+="_"+count.tostring();
break;
}
else
{
count++;
}
}
}
system.io.directory.createdirectory(workpath);
#endregion
system.io.filestream f=new system.io.filestream(ofd.filename,system.io.filemode.open);
system.io.binaryreader r = new system.io.binaryreader(f);
long l=f.length;long x=0;
int filecount=1;
for(;x<l;)
{
system.io.filestream f2=new system.io.filestream(workpath+"//"+filecount.tostring(),system.io.filemode.createnew);
for(int for1=0;for1<i;for1++)
{
if(x<l)
{
f2.writebyte(r.readbyte());
}
x++;
}
f2.close();
filecount++;
}
f.close();
system.io.streamwriter t =new system.io.streamwriter(system.io.file.open(workpath+"//"+"组合文件.bat",system.io.filemode.create),system.text.encoding.default);
t.writeline("@echo off");
t.write("copy ");
int tempcount = 0;
for(int for2=1;for2<filecount;for2++)
{
t.write(for2.tostring()+" /b");
if(for2+1!=filecount)
{
if ((for2 % 100) == 0)
{
t.write(" " + "temp" + ((int)(tempcount++)).tostring());
t.writeline();
t.write("copy ");
}
else
{
t.write(" + ");
}
}
else{
t.write(" " + "temp" + ((int)(tempcount++)).tostring());
t.writeline();
}
}
t.write("copy ");
for (int for2 = 0; for2 < tempcount; for2++)
{
t.write("temp" + for2.tostring() + " /b");
if (for2 + 1 != tempcount && tempcount!=0)
{
t.write(" + ");
}
else{
t.write(" "+getfilename(ofd.filename));
t.writeline();
}
}
for (int for2 = 0; for2 < tempcount; for2++)
{
t.writeline("del " + "temp" + for2.tostring());
}
for(int for2=1;for2<filecount;for2++)
{
t.writeline("del "+for2);
}
t.close();
filecount-=1;
system.windows.forms.messagebox.show("拆分文件创建已经完成,共拆分为"+filecount.tostring()+"部分");
}
catch(system.exception ee)
{
system.windows.forms.messagebox.show("操作出现失误,没有完成拆分:"+ee.message );
gc.collect();
}
}
private string getfilename(string str)
{
int firstname=0;
string result="";
for(int i=str.length-1;i>=0;i--)
{
if(str[i]=='//')
{
firstname=i;
break;
}
}
for(int i=firstname+1;i<str.length;i++)
{
result+=str[i];
}
return result;
}
}
}