ICS TFTPClient 的相关操作。

unit UindexFTP;
interface
uses
SysUtils, Classes ,StrUtils ,OverbyteIcsWndControl, OverbyteIcsFtpCli;
type
TFindFile = procedure(FileName,EditTime,Attribute,Owner,Group,Size:string) of object;
TFindDir = procedure(DirName,EditTime,Attribute,Owner,Group:string) of object;
TOnMessage = procedure(msg:string) of object;
TOnTimeOut = procedure(status:integer) of object;
CUindexFTP = class(TComponent)
private
//来自ICS的TFTPclient控件,基本上成了Winsock的实现
//结果我不得不写一个更加友好的FTP组件UindexFTP
MySpider : TFtpClient;
Fusername : string;
Fpassword : string;
Fserverport : integer;
Fserver : string;
Fversion : string;
FWorkDir : string;
FFindFile : TFindFile;
FFindDir : TFindDir;
FOnMessage : TOnMessage;
FOnTimeOut : TOnTimeOut;
procedure Display(Sender: TObject; var Msg: String);
procedure Error(Sender: TObject; var Msg: String);
procedure StateChange(Sender: TObject);
procedure ParseList(List:string);
protected
Ftimeout : integer;
FConnTimeOut : integer;
Fstatus : integer;
public
constructor Create(Owner: TComponent); override;
destructor Destroy; override;
function connect():boolean;
function ChangeDir(dir:string):boolean;
function ListDir():integer;
published
property TimeOut : integer Read Ftimeout Write Ftimeout;
property Host : string Read Fserver Write Fserver;
property User : string Read Fusername Write Fusername;
Property Pass : string Read Fpassword Write Fpassword;
property Port : integer Read Fserverport Write Fserverport;
property ConnTimeOut : integer read FConnTimeOut Write FConnTimeOut;
property OnFindFile : TFindFile Read FFindFile write FFindFile;
property OnFindDir : TFindDir read FFindDir write FFindDir;
Property CurrentDir : string read FWorkDir write FWorkDir;
property OnMessage : TOnMessage read FOnMessage write FOnMessage;
property OnTimeOut : TOnTimeOut read FOnTimeOut write FOnTimeOut;
property Status : integer read Fstatus write Fstatus;
Property version : string read Fversion;
end;
procedure Register;
implementation
procedure Register;
begin
RegisterComponents('FPiette', [CUindexFTP]);
end;
{ CUindexFTP }
function CUindexFTP.ChangeDir(dir: string): boolean;
begin
result:=false;
if MySpider.Connected then begin
MySpider.HostDirName:=dir;
FWorkDir:=dir;
result:=MySpider.Cwd;
end else begin
if assigned(OnTimeOut) then OnTimeOut(MySpider.StatusCode);
end;
end;
function CUindexFTP.connect: boolean;
begin
result :=false;
MySpider.HostName:=Fserver;
MySpider.UserName:=Fusername;
MySpider.PassWord:=Fpassword;
MySpider.Port:=IntToStr(Fserverport);
MySpider.Timeout:=Ftimeout;
MySpider.MultiThreaded:=true;
if MySpider.Open then
if MySpider.User then
result :=MySpider.Pass;
end;
constructor CUindexFTP.Create(Owner: TComponent);
begin
inherited Create(Owner);
MySpider:=TFtpClient.Create(nil);
MySpider.OnDisplay:=Display;
MySpider.OnError:=Error;
MySpider.OnStateChange:=StateChange;
Fserverport:=21;
Fusername:='anonymous';
Fversion :='UindexFTP V3.0';
Ftimeout :=10;
FConnTimeOut:=10;
end;
destructor CUindexFTP.Destroy;
begin
MySpider.Abort;
MySpider.Free;
inherited Destroy;
end;
procedure CUindexFTP.Display(Sender: TObject; var Msg: String);
begin
if assigned(OnMessage) then OnMessage(Msg);
end;
procedure CUindexFTP.Error(Sender: TObject; var Msg: String);
begin
if assigned(OnMessage) then OnMessage(Msg);
end;
function CUindexFTP.ListDir: integer;
var stm:TMemoryStream;
mylist:TStringList;
buffer:string;
ItemCount:integer;
begin
result:=0;
if MySpider.Connected then begin
stm:=TMemoryStream.Create;
mylist:=TStringList.Create;
try
MySpider.LocalStream:=stm;
if MySpider.Dir then begin
setlength(buffer,stm.size);
stm.Seek(0, soFromBeginning);
stm.Read(buffer[1],stm.size);
mylist.Text:=buffer;
for ItemCount := 0 to mylist.Count-1 do
begin
ParseList(mylist[ItemCount]);
end;
end;
finally
MySpider.LocalStream:=nil;
stm.Free;
mylist.Free;
end;
end else begin
if assigned(OnTimeOut) then OnTimeOut(MySpider.StatusCode);
end;
end;
procedure CUindexFTP.ParseList(List: string);
var i,j,k:integer;
Line,FileDate,FileName,FileAttribute,Owner,Group,FileSize:string;
begin
if List<>'' then begin
Line:=List;
if Line[1] in ['0'..'9'] then begin
//WinNT FTP Service
//微软的IIS附带的 FTP 服务器 DOS 响应选中
i:=pos(#32,Line);
FileDate:=Copy(Line,1,i-1);
Line:=Trim(Copy(Line,i+1,length(Line)-i));
j:=pos(#32,Line);
FileDate:=FileDate+' '+Copy(Line,1,j-1);
Line:=Trim(Copy(Line,j+1,length(Line)-j));
k:=pos('>',Line);
if k>0 then begin
//发现的是目录
FileName:=Trim(Copy(Line,k+1,length(Line)-k));
if Assigned(OnFindDir) then OnFindDir(FileName,FileDate,'','','');
end else begin
//发现的是文件
k:=pos(#32,Line);
FileSize:=Copy(Line,1,k-1);
FileName:=Trim(Copy(Line,k+1,length(Line)-k));
if Assigned(OnFindFile) then OnFindFile(FileName,FileDate,'','','',FileSize);
end;
end else begin
//UNIX SVR 4 或其兼容服务器 目录列表格式:
//drwxr-xr-x 6 1001 1001 512 Jan 19 2006 download
//属性 保留 用户 组 大小 时间 文件名
i:=pos(#32,Line);
FileAttribute:=Copy(Line,1,i-1);
Line:=Trim(Copy(Line,i+1,length(Line)-i));
//6 1001 1001 512 Jan 19 2006 download
i:=pos(#32,Line);
//保留
Line:=Trim(Copy(Line,i+1,length(Line)-i));
//1001 1001 512 Jan 19 2006 download
i:=pos(#32,Line);
Owner:=Copy(Line,1,i-1);
Line:=Trim(Copy(Line,i+1,length(Line)-i));
//1001 512 Jan 19 2006 download
i:=pos(#32,Line);
Group:=Copy(Line,1,i-1);
Line:=Trim(Copy(Line,i+1,length(Line)-i));
//512 Jan 19 2006 download
i:=pos(#32,Line);
FileSize:=Copy(Line,1,i-1);
Line:=Trim(Copy(Line,i+1,length(Line)-i));
//-------------------------------------------------------------------
//Jan 19 2006 download
//查找 3 次空格,做trim即可得到文件名
//-------------------------------------------------------------------
i:=pos(#32,Line);
FileDate:=Copy(Line,1,i-1);
Line:=Trim(Copy(Line,i+1,length(Line)-i));
i:=pos(#32,Line);
FileDate:=FileDate +' '+ Copy(Line,1,i-1);
Line:=Trim(Copy(Line,i+1,length(Line)-i));
i:=pos(#32,Line);
FileDate:=FileDate +' '+ Copy(Line,1,i-1);
FileName:=Trim(Copy(Line,i+1,length(Line)-i));
if LowerCase(List[1])='d' then begin
if Assigned(OnFindDir) then OnFindDir(FileName,FileDate,FileAttribute,Owner,Group);
end else begin
if Assigned(OnFindFile) then OnFindFile(FileName,FileDate,FileAttribute,Owner,Group,FileSize);
end;
end;
end;
end;
procedure CUindexFTP.StateChange(Sender: TObject);
begin
Fstatus:=MySpider.StatusCode;
end;
end.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
最新版的ics支持d7-xe10.1,还支持OpenSSL-Win32 主要控件请见OverbyteIcsReg.pas uses {$IFDEF FMX} FMX.Types, Ics.Fmx.OverbyteIcsWndControl, Ics.Fmx.OverbyteIcsWSocket, Ics.Fmx.OverbyteIcsDnsQuery, Ics.Fmx.OverbyteIcsFtpCli, Ics.Fmx.OverbyteIcsFtpSrv, Ics.Fmx.OverbyteIcsMultipartFtpDownloader, Ics.Fmx.OverbyteIcsHttpProt, Ics.Fmx.OverbyteIcsHttpSrv, Ics.Fmx.OverbyteIcsMultipartHttpDownloader, Ics.Fmx.OverbyteIcsHttpAppServer, Ics.Fmx.OverbyteIcsCharsetComboBox, Ics.Fmx.OverbyteIcsPop3Prot, Ics.Fmx.OverbyteIcsSmtpProt, Ics.Fmx.OverbyteIcsNntpCli, Ics.Fmx.OverbyteIcsFingCli, Ics.Fmx.OverbyteIcsPing, {$IFDEF USE_SSL} Ics.Fmx.OverbyteIcsSslSessionCache, Ics.Fmx.OverbyteIcsSslThrdLock, {$ENDIF} Ics.Fmx.OverbyteIcsWSocketE, Ics.Fmx.OverbyteIcsWSocketS, {$ENDIF FMX} {$IFDEF VCL} Controls, OverbyteIcsWndControl, OverbyteIcsWSocket, OverbyteIcsDnsQuery, OverbyteIcsFtpCli, OverbyteIcsFtpSrv, OverbyteIcsMultipartFtpDownloader, OverbyteIcsHttpProt, OverbyteIcsHttpSrv, OverbyteIcsMultipartHttpDownloader, OverbyteIcsHttpAppServer, OverbyteIcsCharsetComboBox, OverbyteIcsPop3Prot, OverbyteIcsSmtpProt, OverbyteIcsNntpCli, OverbyteIcsFingCli, OverbyteIcsPing, {$IFDEF USE_SSL} OverbyteIcsSslSessionCache, OverbyteIcsSslThrdLock, {$ENDIF} OverbyteIcsWSocketE, OverbyteIcsWSocketS, OverbyteIcsSysLogClient, OverbyteIcsSysLogServer, OverbyteIcsSnmpCli, OverbyteIcsSmtpSrv, // VCL only OverbyteIcsMultiProgressBar, OverbyteIcsEmulVT, OverbyteIcsTnCnx, OverbyteIcsTnEmulVT, OverbyteIcsTnScript, {$IFNDEF BCB} OverbyteIcsWSocketTS, {$ENDIF} {$ENDIF VCL} {$IFDEF ICS_COMMON} OverbyteIcsMimeDec, OverbyteIcsMimeUtils, OverbyteIcsTimeList, OverbyteIcsLogger, {$IFNDEF BCB} OverbyteIcsCookies, {$ENDIF !BCB} {$ENDIF} {$IFDEF RTL_NAMESPACES}System.SysUtils{$ELSE}SysUtils{$ENDIF}, {$IFDEF RTL_NAMESPACES}System.Classes{$ELSE}Classes{$ENDIF}; procedure Register; implementation
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值