在Delphi中新建一个工程,然后在窗体中加入两个BUTTON,一个EDIT,一个OPENDIALOG,一个SAVEDIALOG,再将BUTTON1的CAPTION改为"切割",BUTTON2的CAPTION改为"组合"(注:其它控件的属性略)。再双击BUTTON1和BUTTON2,写下如下代码: //切割文件 PRocedure TForm1.Button1Click(Sender: TObject); var i, WantedFragSize, RealToWrite : Integer; //声明两个文件流对象 //InStream为输入源文件流对象,OutStream为输出切割文件流对象 InStream, OutStream : TFileStream; S : String; begin if Opendialog1.Execute then begin SaveDialog1.Title := '输入你要保存的切割文件名'; If SaveDialog1.Execute then begin WantedFragSize := StrtoInt(Edit1.Text); //定义切割块大小 i := 0; //以只读的方式创建一个输入文件流对象 InStream:=TFileStream.Create( OpenDialog1.FileName,fmOpenRead); Try //假如Instream流的当前指针位置不在末尾,就按照定义的块大小//来读出数据并存成文件 while (InStream.Position < InStream.Size) do Begin s := IntToStr(I); while Length(s) < 3 do s := '0'+s; s := '.'+s; //生成切割文件的扩展名,用三位数字来作为扩展名,方便组合文件 //假如InStream所剩的块大小小于定义的块大小,则将剩下的块数//据存成文件 If InStream.Size - InStream.Position < WantedFragSize then RealToWrite := InStream.Size - InStream.Position else RealToWrite := WantedFragSize; //创建一个输出文件流对象 OutStream:=TFileStream.Create(SaveDialog1.FileName+s,fmCreate); try OutStream.CopyFrom(InStream,RealToWrite); Inc(i); Finally //释放输出文件流对象 OutStream.Free; end; end; Finally InStream.Free; end; end; end; end; //组合文件 procedure TForm1.Button2Click(Sender: TObject); var i : Integer; InStream, OutStream : TFileStream; SourcePath, FragName, S : String; begin Opendialog1.Title := '请选择切割文件'; if Opendialog1.Execute then begin //得到当前打开的切割文件的路径 SourcePath := ExtractFilePath(Opendialog1.FileName); //得到当前打开的切割文件的文件名 FragName := ExtractFileName(Opendialog1.FileName); SaveDialog1.Title := '请输入你要组合的文件名'; If SaveDialog1.Execute then begin i := 0; s := IntToStr(I); while Length(s) < 3 do s := '0'+s; s := '.'+s; FragName := ChangeFileExt(FragName, s); //假如目录中有以.000为扩展名的文件存在,则创建一个输出文件流//对象 If FileExists(SourcePath+FragName) then begin OutStream:=TFileStream.Create(Savedialog1.FileName,fmCreate); Try //按照以文件扩展名递增的方式组合文件 While FileExists(SourcePath+FragName) do Begin InStream:=TFileStream.Create(SourcePath+FragName,fmOpenRead); Try //将输入文件流的数据写入到输出文件流中 OutStream.CopyFrom(InStream,0); Inc(i); s := IntToStr(I); while Length(s) < 3 do s := '0'+s; s := '.'+s; FragName := ChangeFileExt(FragName, s); finally InStream.Free; end; end; finally OutStream.Free; end; end; end; end; end; {*注释:请注重COPYFROM方法的使用,当第二个参数为0时,表示将输入流InStream的数据全部写入输出流OutStream;当大于0为N时,表示从输入流InStream的当前流指针位置开始,将N个字节数据写入输出流OutStream,并且InStream的流指针会向后偏移N个字节位数。具体解释请查看DELPHI自带的帮助*} 三、结束语