使用ftp控件下载一个目录2
上次写了篇“使用ftp控件下载一个目录”,里面写的目录下载函数在nt下使用正常,但升级到2000下不能正常使用。将windows 2000的ftp站点属性中的目录列表风格改为unix也不行。我认为是delphi自带的NMFTP控件太老,对2000支持不好的原因。我在提供该控件的公司的网站上想下载一个最新版本,发现要交$199.95费用!没办法,干脆不用控件,直接用微软提供的相关ftp函数直接来完成。使用进栈和出栈的原理,有目录和文件就进栈,下载后就出栈,等到栈空的时候也就是整个目录(包含各级子目录)下载完毕的时候。整个目录下载的函数如下,有兴趣着可参考一下:
function Tmyftp.dir_download(remotedir:string;localdir:string):Boolean;var dirstack: TStack; prtdir,nowprtdir: PDirstru; dirover : Boolean;begin result := false; dirstack := TStack.create; new(prtdir); if prtdir<>Nil then begin prtdir.ftpdirstr := remotedir; prtdir.localdirstr := localdir; prtdir.finishflag := false; dirstack.Push(prtdir); if not directoryexists(localdir) then createdir(localdir); end;
while dirstack.count>0 do begin nowprtdir := dirstack.pop; change_dir(nowprtdir^.ftpdirstr); ListHandle := FtpFindFirstFile(FtpHandle,'*.*',FindData,0,0); if ListHandle <> Nil then begin dirover := true; while dirover do begin if finddata.dwFileAttributes = FILE_ATTRIBUTE_DIRECTORY then begin new(prtdir); if prtdir <> Nil then begin prtdir.ftpdirstr := nowprtdir^.ftpdirstr+finddata.cfilename+'/'; prtdir.localdirstr := nowprtdir^.localdirstr+finddata.cfilename+'/'; prtdir.finishflag := false; dirstack.Push(prtdir); if not directoryexists(nowprtdir^.localdirstr+trim(finddata.cfilename)) then createdir(nowprtdir^.localdirstr+trim(finddata.cfilename)); end else begin errno := 4; errmsg := '在目录download过程中不能申请得到内存。'; raise FtpException.create(errmsg); end end else begin try if not FtpGetFile(FtpHandle,pchar(nowprtdir^.ftpdirstr+finddata.cfilename), pchar(nowprtdir^.localdirstr+finddata.cfilename),false,0,0,0) then begin errno := 5; errmsg := '在download文件 '+nowprtdir^.ftpdirstr+finddata.cfilename+' 时发生错误。'; raise FtpException.create(errmsg); end; except on exception do begin errno := 6; errmsg := '在download文件 '+nowprtdir^.ftpdirstr+finddata.cfilename+' 时发生例外错误。'; raise FtpException.create(errmsg); end; end end;
dirover := InternetFindNextFile(ListHandle,@finddata) end; InternetCloseHandle(ListHandle); end else begin end; dispose(nowprtdir); end; end;
http://www.softcows.com/cn