荔园在线

荔园之美,在春之萌芽,在夏之绽放,在秋之收获,在冬之沉淀

[回到开始] [上一篇][下一篇]


发信人: oopilix (优雅的), 信区: Visual
标  题: [zl]split a file with stream method in delphi
发信站: 荔园晨风BBS站 (Sun Jun  1 23:26:53 2003), 站内信件

a sample from my paq:

a simple sample

unit splitfiles_u;

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
OpenDialog1: TOpenDialog;
Edit1: TEdit;
SaveDialog1: TSaveDialog;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.DFM}

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

end.

--
※ 来源:·荔园晨风BBS站 bbs.szu.edu.cn·[FROM: 61.144.235.39]


[回到开始] [上一篇][下一篇]

荔园在线首页 友情链接:深圳大学 深大招生 荔园晨风BBS S-Term软件 网络书店