delphi 用文件流拷贝文件的问题???//(100分)

  • 主题发起人 主题发起人 hcnet
  • 开始时间 开始时间
H

hcnet

Unregistered / Unconfirmed
GUEST, unregistred user!
procedure TForm1.Button1Click(Sender: TObject);

var
S,T: TFileStream; {文件流}
SourceFileName,DestFileName:String;{源文件和目的文件名}
//IniFile:TIniFile;
FileName:string;
begin
SourceFileName:='e:/sys/ghost.iso';
DestFileName:='d:/sys/ghost.iso';
S:=TFileStream.Create(SourceFileName, fmOpenRead );
Application.ProcessMessages;
try
T:= TFileStream.Create(DestFileName,fmOpenWrite or fmCreate );
try
T.CopyFrom(S,S.Size)
T.Free;
showmessage('拷贝成功!');
except
showmessage('拷贝失败,请手工拷贝!');
end;
S.Free;
except
showmessage('拷贝失败,请手工拷贝!');
end;
end;
为什么拷贝 超过1G的文件会出现假死的状态。。。。系统无响应。。。
死循环现象

下面用内存流拷贝的时候也是这样的。。

procedure TForm1.Button1Click(Sender: TObject);

var

SourceFileName,DestFileName: String;

begin
SourceFileName:='e:/sys/ghost.iso';
DestFileName:='d:/sys/ghost.iso';
with TMemoryStream.Create do

try

LoadFromFile(SourceFileName);

SaveToFile(DestFileName);

finally

Free;
end;
end;

end.

请问各位学长有没有好的办法能够解决呢?
Application.ProcessMessages; 这个加上话的。。。

能解决这个问题??
用文件流和内存流拷贝的时候。。。进度条该如何解决呢?
 
把这段代码放到线程中去执行。
 
还请楼上的兄台。。。

能否详细指点一下。。。。。。。对线程 没做过不是很熟悉。。
 
http://www.delphibbs.com/delphibbs/dispq.asp?lid=1498605
 
兄弟在这里就先帮你把分数加上。。。我要就是这个代码。
楼上的兄弟能否麻烦注解一下。。因为有很多控件定义改名过的。不知道
比如这个:public
Percent : Integer;
Done,ToDo : Integer;
我无法编译。。。。。。不知兄弟能否帮忙。。。。


参考:

TCopyFile = class(TThread)
public
Percent : Integer;
Done,ToDo : Integer;
ListIndex : integer;
Start : TDateTime;
constructor Create(Src, Dest: String);
private
{ Private declarations }
IName,OName : String;
protected
procedure Execute; override;
procedure CopyProgress;
procedure TotalCopyProgress;
procedure ShowError;
end;

const
sc_DragMove:longint=$F012;
KB1 = 1024;
MB1 = 1024*KB1;
GB1 = 1024*MB1;
// ---------------------------
del_img_set=[22,24,25];
copy_img_set=[22,25];

var
CopyToForm: TCopyToForm;
iCopy,wid:integer;

implementation
uses Main,ShellAPI,FileCtrl,FmxUtils,ShlObj,ActiveX;
{$R *.DFM}

constructor TCopyFile.Create(Src, Dest : String);
begin
IName := Src;
OName := Dest;
Percent := 0;
Start := Now;
FreeOnTerminate := True;
inherited Create(True);
end;

procedure TCopyFile.ShowError;
begin
CopyToForm.CopyListView.items[ListIndex].ImageIndex :=25;
ShowMessage('无法读取源文件'+IName+',此次拷贝将是不完整的,请以后再试。');
end;

procedure TCopyFile.CopyProgress;
begin
with CopyToForm do
begin
//listview1.items[ListIndex].SubItems.BeginUpdate;
CopyListview.items[ListIndex].SubItems[0]:= inttostr(Percent)+'%';
if percent>=100 then CopyListview.items[ListIndex].ImageIndex :=24;
//label4.Caption := '已复制'+ inttostr(Round((ListIndex+1)/listview1.Items.Count*100))+'%';
//listview1.items[ListIndex].SubItems.EndUpdate;
end;
end;

procedure TCopyFile.TotalCopyProgress;
begin
with CopyToForm do
begin
inc(iCopy);
label4.Caption := '已复制'+ inttostr(Round((iCopy)/CopyListview.Items.Count*100))+'%';
caption:='拷贝文件('+label4.Caption+')';
if iCopy=CopyListview.Items.Count then
begin
label4.Caption := '复制完成。';
BtnCancel.Caption :='关闭';
//speedbutton4.Enabled := false;
show;
CopyToForm.WindowState := wsNormal;
//listview1.items[ListIndex].SubItems.EndUpdate;
end;
end;
end;

procedure TCopyFile.Execute;
var
fi,fo : TFileStream;
dod,did : Integer;
cnt,max : Integer;
begin
Start := Now;
//try
{ Open existing destination }
if fileexists(oName) then //断点续传!
begin
try
fo := TFileStream.Create(OName, fmOpenReadWrite);
except on EFOpenError do
begin
{CopyToForm.CopyListView.items[ListIndex].ImageIndex :=25;
ShowMessage('无法读取源文件'+OName+',此次拷贝将是不完整的,请以后再试。'); }
synchronize(ShowError);
exit;
end;
end;//end of try
fo.Position:=fo.size;
end
//except
{ otherwise Create destination }
else fo := TFileStream.Create(OName, fmCreate);
//end;
try
{ open source }
try
fi := TFileStream.Create(IName, fmOpenRead);
except on EFOpenError do
begin
synchronize(ShowError);
exit;
end;
end;//end of try

try
{ synchronise dest en src }
cnt:= fo.Position;
fi.Position := cnt;
max := fi.Size;
ToDo := Max-cnt;
Done := 0;
did:=0; // zw
{ start copying }
Repeat
dod := KB1; // Block size
if cnt+dod>max then dod := max-cnt;
try
if dod>0 then did := fo.CopyFrom(fi, dod);
except on EReadError do
begin
{CopyToForm.CopyListView.items[ListIndex].ImageIndex :=25;
ShowMessage('无法读取源文件'+OName+',此次拷贝将是不完整的,请以后再试。');}
synchronize(ShowError);
exit;
end
end; // end of try
cnt:=cnt+did;
Percent := Round(Cnt/Max*100);
synchronize(CopyProgress);
Done := Done+did;
ToDo := Max;
until (dod=0) or (Terminated);

finally
fi.free;
end;
finally
fo.free;
end;
synchronize(TotalCopyProgress);
end;

这个方法实际上支持断点序传哟(用于局域网中文件复制时特别有用)
 
d->new->other->thread object->class name填TCopyFile->确定 再把下面的代码贴进去
在主程序中启动线程会吧?要是不会请去搜搜离线包对于初学者来说离线包可能是最好的老师了,呵呵
unit Unit2;

interface

uses
Classes,SysUtils;

type
TCopyFile = class(TThread)
public
Percent : Integer;
Done,ToDo : Integer;
ListIndex : integer;
Start : TDateTime;
constructor Create(Src, Dest: String);
private
{ Private declarations }
IName,OName : String;
protected
procedure Execute; override;
procedure CopyProgress;
procedure TotalCopyProgress;
procedure ShowError;
end;

const
sc_DragMove:longint=$F012;
KB1 = 1024;
MB1 = 1024*KB1;
GB1 = 1024*MB1;
// ---------------------------
del_img_set=[22,24,25];
copy_img_set=[22,25];

var
// CopyToForm: TCopyToForm;
iCopy,wid:integer;

implementation

constructor TCopyFile.Create(Src, Dest : String);
begin
IName := Src;
OName := Dest;
Percent := 0;
Start := Now;
FreeOnTerminate := True;
inherited Create(True);
end;

procedure TCopyFile.ShowError;
begin
// CopyToForm.CopyListView.items[ListIndex].ImageIndex :=25;
// ShowMessage('无法读取源文件'+IName+',此次拷贝将是不完整的,请以后再试。');
end;

procedure TCopyFile.CopyProgress;
begin
// with CopyToForm do
begin
//listview1.items[ListIndex].SubItems.BeginUpdate;
// CopyListview.items[ListIndex].SubItems[0]:= inttostr(Percent)+'%';
// if percent>=100 then CopyListview.items[ListIndex].ImageIndex :=24;
//label4.Caption := '已复制'+ inttostr(Round((ListIndex+1)/listview1.Items.Count*100))+'%';
//listview1.items[ListIndex].SubItems.EndUpdate;
end;
end;

procedure TCopyFile.TotalCopyProgress;
begin
{ with CopyToForm do
begin
inc(iCopy);
label4.Caption := '已复制'+ inttostr(Round((iCopy)/CopyListview.Items.Count*100))+'%';
caption:='拷贝文件('+label4.Caption+')';
if iCopy=CopyListview.Items.Count then
begin
label4.Caption := '复制完成。';
BtnCancel.Caption :='关闭';
//speedbutton4.Enabled := false;
show;
CopyToForm.WindowState := wsNormal;
//listview1.items[ListIndex].SubItems.EndUpdate;
end;
end;}
end;

procedure TCopyFile.Execute;
var
fi,fo : TFileStream;
dod,did : Integer;
cnt,max : Integer;
begin
Start := Now;
//try
{ Open existing destination }
if fileexists(oName) then //断点续传!
begin
try
fo := TFileStream.Create(OName, fmOpenReadWrite);
except on EFOpenError do
begin
{CopyToForm.CopyListView.items[ListIndex].ImageIndex :=25;
ShowMessage('无法读取源文件'+OName+',此次拷贝将是不完整的,请以后再试。'); }
synchronize(ShowError);
exit;
end;
end;//end of try
fo.Position:=fo.size;
end
//except
{ otherwise Create destination }
else fo := TFileStream.Create(OName, fmCreate);
//end;
try
{ open source }
try
fi := TFileStream.Create(IName, fmOpenRead);
except on EFOpenError do
begin
synchronize(ShowError);
exit;
end;
end;//end of try

try
{ synchronise dest en src }
cnt:= fo.Position;
fi.Position := cnt;
max := fi.Size;
ToDo := Max-cnt;
Done := 0;
did:=0; // zw
{ start copying }
Repeat
dod := KB1; // Block size
if cnt+dod>max then dod := max-cnt;
try
if dod>0 then did := fo.CopyFrom(fi, dod);
except on EReadError do
begin
{CopyToForm.CopyListView.items[ListIndex].ImageIndex :=25;
ShowMessage('无法读取源文件'+OName+',此次拷贝将是不完整的,请以后再试。');}
synchronize(ShowError);
exit;
end
end; // end of try
cnt:=cnt+did;
Percent := Round(Cnt/Max*100);
synchronize(CopyProgress);
Done := Done+did;
ToDo := Max;
until (dod=0) or (Terminated);

finally
fi.free;
end;
finally
fo.free;
end;
synchronize(TotalCopyProgress);
end;

end.
 

Similar threads

后退
顶部