DELPHI切割文件代码

来源:互联网 发布:网络协同 系统架构 编辑:程序博客网 时间:2024/04/30 01:45
//Splitting Files
procedure TForm1.Button1Click(Sender: TObject);
var
i, WantedFragSize, RealToWrite : Integer;
InStream, OutStream : TFileStream;
S : String;
begin
if Opendialog1.Execute then
begin
 SaveDialog1.Title := 'Select Dir and input FilePrefix without Suffix';
 If SaveDialog1.Execute then
 begin
   WantedFragSize := StrtoInt(Edit1.Text);  //Wanted SplitSize
   i := 0;
   InStream  := TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
   try
     while (InStream.Position < InStream.Size) do
     Begin
       s := IntToStr(I);                   //Building numeric suffix
       while Length(s) < 3 do s := '0'+s;
       s := '.'+s;
       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;

//concanating files
procedure TForm1.Button2Click(Sender: TObject);
var
i : Integer;
InStream, OutStream : TFileStream;
SourcePath, FragName, S : String;
begin
Opendialog1.Title := 'Select any Fragmented File';
if Opendialog1.Execute then
begin
 SourcePath := ExtractFilePath(Opendialog1.FileName);
 FragName := ExtractFileName(Opendialog1.FileName);
 SaveDialog1.Title := 'Select Dir and input the FileName with Suffix';
 If SaveDialog1.Execute then
 begin
   i := 0;
   s := IntToStr(I);
   while Length(s) < 3 do s := '0'+s;
   s := '.'+s;
   FragName := ChangeFileExt(FragName, s);
   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;