Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- unit ATTTerm.EngineFtp;
- interface
- uses
- Classes,
- IdFTP,
- SysUtils,
- idftpcommon,
- System.Generics.Collections,
- System.Zip,
- Variants,
- Vcl.dialogs,
- Messages,
- Controls,
- windows,
- Forms;
- type TDespoisdeExtrairArquivos = procedure of object;
- type TAntesdeExtrairArquivos = procedure of object;
- type TAtttermEngineFtp = class( TThread )
- private
- {Private declaration}
- Ftp : TidFTP;
- FPassword: string;
- FHost: string;
- FUserName: string;
- FCaminhoDiretorioFtp: string;
- FPastaDescompactacaoZip: string;
- FCaminhoDiretorioLocal: string;
- vArquiname: string;
- vArquiDateModifiqued: TDateTime;
- FTDespoisdeExtrairArquivos: TDespoisdeExtrairArquivos;
- FTAntesdeExtrairArquivos: TAntesdeExtrairArquivos;
- FException: Exception;
- function extractarquivosFtptoCaminhoTmp( aIdFtp: TIDFtp; const aPastaDescompactacaoZip,
- aCaminhoDiretorioLocal,aCaminhoPastaFtp: string ): Boolean; inline;
- function PercorreArquivosDiretorio( const aDiretorioLocal,arqNameDirectoryFtp: String ): string; inline;
- procedure DoHandleException;
- protected
- {Protected declaration}
- procedure HandleException; virtual;
- public
- procedure AfterConstruction; override;
- procedure BeforeDestruction; override;
- {Public declaration declaration}
- property Host: string read FHost write FHost;
- property UserName: string read FUserName write FUserName;
- property password: string read FPassword write FPassword;
- function EfetuaConexaocomservidor( aIdFtp: TIDFtP ): Boolean; register;
- function PercorreListaFtp( aIdFtp: TIDFtp; const aPastaDesCompactacaoZip,aCamihoLocal,
- aDiretorioFtp: string ): Boolean; inline;
- property DepoisdeExtrairArquivos: TDespoisdeExtrairArquivos read FTDespoisdeExtrairArquivos write FTDespoisdeExtrairArquivos;
- property AntesdeExtrairArquivos: TAntesdeExtrairArquivos read FTAntesdeExtrairArquivos write FTAntesdeExtrairArquivos;
- constructor Create( const DiretorioFtp,aPastaDesCompactacaoZip,aCaminhoExecutaveisLocal,aHostname,
- anomeuser,apassword: string; aprogress: integer );
- destructor Destroy; override;
- procedure Execute; override;
- published
- {Protected declaration}
- end;
- implementation
- { TAtttermEngineFtp }
- procedure TAtttermEngineFtp.AfterConstruction;
- begin
- inherited AfterConstruction;
- end;
- procedure TAtttermEngineFtp.BeforeDestruction;
- begin
- inherited BeforeDestruction;
- end;
- constructor TAtttermEngineFtp.Create( const DiretorioFtp,aPastaDesCompactacaoZip,aCaminhoExecutaveisLocal,aHostname,
- anomeuser,apassword: string; aprogress: integer );
- begin
- inherited Create( true );
- FreeOnTerminate := True;
- FCaminhoDiretorioFtp := DiretorioFtp;
- FPastaDescompactacaoZip := aPastaDesCompactacaoZip;
- FCaminhoDiretorioLocal := aCaminhoExecutaveisLocal;
- FHost := aHostname;
- FUserName := anomeuser;
- FPassword := apassword;
- if Not Assigned( Ftp ) then
- begin
- Ftp := TIdFTP.Create( nil );
- end;
- // Resume;
- end;
- destructor TAtttermEngineFtp.Destroy;
- begin
- inherited;
- end;
- procedure TAtttermEngineFtp.DoHandleException;
- begin
- if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
- if FException is Exception then
- Application.ShowException(FException)
- else
- SysUtils.ShowException(FException, nil);
- end;
- function TAtttermEngineFtp.EfetuaConexaocomservidor( aIdFtp: TIDFtP ): Boolean;
- begin
- aIdFtp.Host := FHost;
- aIdFtp.Password := FPassword;
- aIdFtp.Username := FUserName;
- try
- aIdFtp.Connect;
- if aIdFtp.Connected then
- begin
- aIdFtp.ChangeDir( FCaminhoDiretorioFtp );
- end;
- except
- on E: Exception do
- raise Exception.Create(E.Message);
- end;
- Result := aIdFtp.Connected;
- end;
- procedure TAtttermEngineFtp.Execute;
- begin
- FException := nil;
- try
- EfetuaConexaocomservidor( Ftp );
- Except
- HandleException;
- Ftp.DisposeOf;
- Terminate;
- raise
- end;
- end;
- function TAtttermEngineFtp.extractarquivosFtptoCaminhoTmp( aIdFtp: TIDFtp; const aPastaDescompactacaoZip,
- aCaminhoDiretorioLocal,aCaminhoPastaFtp: string ): Boolean;
- var i: integer; Descompressor: Tzipfile;
- begin
- if Assigned( FTAntesdeExtrairArquivos ) then
- begin
- FTAntesdeExtrairArquivos();
- end;
- Descompressor:= TZipFile.Create;
- aIdFtp.ChangeDir( aCaminhoPastaFtp );
- aIdFtp.List;
- for I := 0 to aIdFtp.DirectoryListing.Count -1 do
- begin
- try
- aIdFtp.Get( aIdFtp.DirectoryListing[i].FileName,aCaminhoDiretorioLocal,True );
- finally
- Descompressor.ExtractZipFile( aIdFtp.DirectoryListing[i].FileName,aPastaDescompactacaoZip );
- end;
- end;
- Descompressor.DisposeOf;
- if assigned( FTDespoisdeExtrairArquivos ) then
- begin
- FTDespoisdeExtrairArquivos();
- end;
- end;
- procedure TAtttermEngineFtp.HandleException;
- begin
- FException := Exception(ExceptObject);
- try
- if not (FException is EAbort) then
- Synchronize(DoHandleException);
- finally
- FException := nil;
- end;
- end;
- function TAtttermEngineFtp.PercorreArquivosDiretorio( const aDiretorioLocal,arqNameDirectoryFtp: String ): string;
- var F: TSearchRec;
- Ret: Integer;
- var Age: integer;
- begin
- Ret := FindFirst( aDiretorioLocal +arqNameDirectoryFtp, faAnyFile, F);
- vArquiname := F.Name;
- Age:= FileAge( vArquiname );
- vArquiDateModifiqued := FileDateToDateTime( Age );
- Result:= vArquiname;
- end;
- function TAtttermEngineFtp.PercorreListaFtp( aIdFtp: TIDFtp; const aPastaDesCompactacaoZip,aCamihoLocal,
- aDiretorioFtp: string ): Boolean;
- var i: integer;
- begin
- aIdFtp.ChangeDir( aDiretorioFtp );
- aIdFtp.List;
- for I := 0 to aIdFtp.DirectoryListing.Count -1 do
- begin
- if aIdFtp.DirectoryListing[i].FileName = PercorreArquivosDiretorio( aCamihoLocal,
- aIdFtp.DirectoryListing[i].FileName ) then
- begin
- extractarquivosFtptoCaminhoTmp( aIdFtp,aPastaDesCompactacaoZip,aCamihoLocal,aDiretorioFtp );
- end;
- end;
- end;
- end.
Add Comment
Please, Sign In to add comment