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;
}
}
}