unit copythread;
interface
uses
Classes, SysUtils;
Const
KB1 = 1024;
MB1 = 1024*KB1;
GB1 = 1024*MB1;
type
TCopyFile = class(TThread)
public
Percent : Integer;
Done,ToDo : Integer;
Start : TDateTime;
constructor Create(Src, Dest: String);
private
{ Private declarations }
IName,OName : String;
protected
procedure Execute; override;
end;
implementation
{ TCopyFile }
constructor TCopyFile.Create(Src, Dest : String);
begin
IName := Src;
OName := Dest;
Percent := 0;
Start := Now;
FreeOnTerminate := True;
inherited Create(True);
end;
procedure TCopyFile.Execute;
var
fi,fo : TFileStream;
dod,did : Integer;
cnt,max : Integer;
begin
Start := Now;
try
{ Открываем существующий путь }
fo := TFileStream.Create(OName, fmOpenReadWrite);
fo.Position:=fo.size;
except
{ иначе создаём путь }
fo := TFileStream.Create(OName, fmCreate);
end;
try
{ открываем копируемый файл }
fi := TFileStream.Create(IName, fmOpenRead);
try
{ синхронизируем }
cnt:= fo.Position;
fi.Position := cnt;
max := fi.Size;
ToDo := Max-cnt;
Done := 0;
{ начинаем копирование }
Repeat
dod := MB1; // Block size
if cnt+dod>max then dod := max-cnt;
if dod>0 then did := fo.CopyFrom(fi,
dod);
cnt:=cnt+did;
Percent := Round(Cnt/Max*100);
Done := Done+did;
ToDo := Max;
until (dod=0) or (Terminated);
finally
fi.free;
end;
finally
fo.free;
end;
end;
end.