Unit1.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:2k
- unit Unit1;
- interface
- uses
- Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
- Dialogs, lzExpand, StdCtrls;
- type
- TForm1 = class(TForm)
- Button1: TButton;
- OpenDialog1: TOpenDialog;
- SaveDialog1: TSaveDialog;
- procedure Button1Click(Sender: TObject);
- private
- { Private declarations }
- public
- { Public declarations }
- end;
- var
- Form1: TForm1;
- implementation
- {$R *.dfm}
- //利用文件流拷贝文件
- procedure FileCopyA(const sourcefilename, targetfilename: string);
- var
- S, T: TFileStream;
- begin
- //创建源文件的文件流
- S := TFileStream.Create(sourcefilename, fmOpenRead);
- try
- //创建目标文件的文件流
- T := TFileStream.Create(targetfilename,
- fmOpenWrite or fmCreate);
- try
- //将源文件流拷贝到目标文件流
- T.CopyFrom(S, S.Size);
- finally
- //释放目标文件流
- T.Free;
- end;
- finally
- //释放源文件流
- S.Free;
- end;
- end;
- //利用内存块读写
- procedure FileCopyB(const FromFile, ToFile: string);
- var
- FromF, ToF: file;
- NumRead, NumWritten: integer;
- Buf: array[1..2048] of Char;
- begin
- //关联源文件
- AssignFile(FromF, FromFile);
- //打开文件,每次只读一个字节
- Reset(FromF, 1);
- //关联目标文件
- AssignFile(ToF, ToFile);
- //创建目标文件,并打开,每次只读一个字节
- Rewrite(ToF, 1);
- //循环读写
- repeat
- BlockRead(FromF, Buf, SizeOf(Buf), NumRead);
- BlockWrite(ToF, Buf, NumRead, NumWritten);
- until (NumRead = 0) or (NumWritten <> NumRead);
- //关闭源文件和目标文件
- CloseFile(FromF);
- CloseFile(ToF);
- end;
- //利用LZCopy函数实现 ,lzCopy函数在LZExpand单元里。
- procedure FileCopyC(FromFileName, ToFileName: string);
- var
- FromFile, ToFile: file;
- begin
- //关联源文件
- AssignFile(FromFile, FromFileName);
- //关联目标文件
- AssignFile(ToFile, ToFileName);
- //打开,源文件
- Reset(FromFile);
- try
- //创建目标文件,并打开
- Rewrite(ToFile);
- try
- //利用LZCopy函数拷贝文件
- if LZCopy(TFileRec(FromFile).Handle, TFileRec(ToFile).Handle) < 0 then
- raise EInOutError.Create('Error using LZCopy')
- finally
- //关闭目标文件
- CloseFile(ToFile);
- end;
- finally
- //关闭源文件
- CloseFile(FromFile);
- end;
- end;
- procedure TForm1.Button1Click(Sender: TObject);
- begin
- if OpenDialog1.Execute then
- begin
- if SaveDialog1.Execute then
- begin
- FileCopyC(openDialog1.FileName, SaveDialog1.FileName);
- end;
- end;
- end;
- end.