//切割文件 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; |