filhotecmail

ATTTerm.EngineFtp;

Jan 20th, 2018
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 6.13 KB | None | 0 0
  1. unit ATTTerm.EngineFtp;
  2.  
  3. interface
  4.  
  5. uses
  6.   Classes,
  7.   IdFTP,
  8.   SysUtils,
  9.   idftpcommon,
  10.   System.Generics.Collections,
  11.   System.Zip,
  12.   Variants,
  13.   Vcl.dialogs,
  14.   Messages,
  15.   Controls,
  16.   windows,
  17.   Forms;
  18.  
  19.  type TDespoisdeExtrairArquivos = procedure  of object;
  20.  type TAntesdeExtrairArquivos   = procedure  of object;
  21.  
  22.  type TAtttermEngineFtp = class( TThread )
  23.     private
  24.     {Private declaration}
  25.     Ftp : TidFTP;
  26.     FPassword: string;
  27.     FHost: string;
  28.     FUserName: string;
  29.     FCaminhoDiretorioFtp: string;
  30.     FPastaDescompactacaoZip: string;
  31.     FCaminhoDiretorioLocal: string;
  32.     vArquiname: string;    
  33.     vArquiDateModifiqued: TDateTime;
  34.     FTDespoisdeExtrairArquivos: TDespoisdeExtrairArquivos;
  35.     FTAntesdeExtrairArquivos: TAntesdeExtrairArquivos;
  36.     FException: Exception;
  37.  
  38.     function extractarquivosFtptoCaminhoTmp( aIdFtp: TIDFtp; const aPastaDescompactacaoZip,
  39.   aCaminhoDiretorioLocal,aCaminhoPastaFtp: string ): Boolean; inline;
  40.     function PercorreArquivosDiretorio( const aDiretorioLocal,arqNameDirectoryFtp: String ): string; inline;
  41.     procedure DoHandleException;
  42.  
  43.     protected
  44.     {Protected declaration}
  45.      procedure HandleException; virtual;
  46.     public
  47.     procedure AfterConstruction; override;
  48.     procedure BeforeDestruction; override;
  49.     {Public declaration declaration}
  50.     property Host: string     read FHost     write FHost;
  51.     property UserName: string read FUserName write FUserName;
  52.     property password: string read FPassword write FPassword;
  53.     function EfetuaConexaocomservidor( aIdFtp: TIDFtP ): Boolean; register;
  54.     function PercorreListaFtp( aIdFtp: TIDFtp; const aPastaDesCompactacaoZip,aCamihoLocal,
  55.           aDiretorioFtp: string ): Boolean; inline;
  56.     property DepoisdeExtrairArquivos: TDespoisdeExtrairArquivos read FTDespoisdeExtrairArquivos write FTDespoisdeExtrairArquivos;
  57.     property AntesdeExtrairArquivos: TAntesdeExtrairArquivos read FTAntesdeExtrairArquivos write FTAntesdeExtrairArquivos;
  58.  
  59.     constructor Create( const DiretorioFtp,aPastaDesCompactacaoZip,aCaminhoExecutaveisLocal,aHostname,
  60.                anomeuser,apassword: string; aprogress: integer );
  61.     destructor Destroy; override;
  62.      procedure Execute; override;
  63.  
  64.  
  65.     published
  66.     {Protected declaration}
  67.     end;
  68.  
  69. implementation
  70.  
  71. { TAtttermEngineFtp }
  72.  
  73. procedure TAtttermEngineFtp.AfterConstruction;
  74. begin
  75.   inherited AfterConstruction;
  76.  
  77. end;
  78.  
  79. procedure TAtttermEngineFtp.BeforeDestruction;
  80. begin
  81.   inherited BeforeDestruction;
  82.  
  83. end;
  84.  
  85. constructor TAtttermEngineFtp.Create( const DiretorioFtp,aPastaDesCompactacaoZip,aCaminhoExecutaveisLocal,aHostname,
  86.                anomeuser,apassword: string; aprogress: integer );
  87. begin
  88.   inherited Create( true );
  89.   FreeOnTerminate := True;
  90.   FCaminhoDiretorioFtp    := DiretorioFtp;
  91.   FPastaDescompactacaoZip := aPastaDesCompactacaoZip;
  92.   FCaminhoDiretorioLocal  := aCaminhoExecutaveisLocal;
  93.   FHost := aHostname;
  94.   FUserName := anomeuser;
  95.   FPassword := apassword;
  96.    if Not Assigned( Ftp ) then
  97.   begin
  98.     Ftp := TIdFTP.Create( nil );
  99.   end;
  100. //  Resume;
  101. end;
  102.  
  103. destructor TAtttermEngineFtp.Destroy;
  104. begin
  105.  
  106.   inherited;
  107. end;
  108.  
  109. procedure TAtttermEngineFtp.DoHandleException;
  110. begin
  111.  
  112.   if GetCapture <> 0 then SendMessage(GetCapture, WM_CANCELMODE, 0, 0);
  113.   if FException is Exception then
  114.     Application.ShowException(FException)
  115.   else
  116.     SysUtils.ShowException(FException, nil);
  117. end;
  118.  
  119. function TAtttermEngineFtp.EfetuaConexaocomservidor( aIdFtp: TIDFtP ): Boolean;
  120. begin
  121.  
  122.   aIdFtp.Host     := FHost;
  123.   aIdFtp.Password := FPassword;
  124.   aIdFtp.Username := FUserName;
  125.  
  126.  try
  127.     aIdFtp.Connect;
  128.      if aIdFtp.Connected then
  129.    begin
  130.      aIdFtp.ChangeDir( FCaminhoDiretorioFtp );
  131.    end;
  132.  except
  133.    on E: Exception do
  134.     raise Exception.Create(E.Message);
  135.  end;
  136.    Result := aIdFtp.Connected;
  137. end;
  138.  
  139. procedure TAtttermEngineFtp.Execute;
  140. begin
  141.   FException := nil;
  142.   try
  143.    EfetuaConexaocomservidor( Ftp );
  144.   Except
  145.    HandleException;
  146.    Ftp.DisposeOf;
  147.    Terminate;
  148.     raise
  149.   end;
  150.  
  151. end;
  152.  
  153. function TAtttermEngineFtp.extractarquivosFtptoCaminhoTmp( aIdFtp: TIDFtp; const aPastaDescompactacaoZip,
  154.   aCaminhoDiretorioLocal,aCaminhoPastaFtp: string ): Boolean;
  155.    var i: integer; Descompressor: Tzipfile;
  156. begin
  157.   if Assigned( FTAntesdeExtrairArquivos ) then
  158.   begin
  159.     FTAntesdeExtrairArquivos();
  160.   end;
  161.  
  162.   Descompressor:= TZipFile.Create;
  163.   aIdFtp.ChangeDir( aCaminhoPastaFtp );
  164.   aIdFtp.List;
  165.   for I := 0 to aIdFtp.DirectoryListing.Count -1 do
  166.    begin
  167.       try
  168.        aIdFtp.Get( aIdFtp.DirectoryListing[i].FileName,aCaminhoDiretorioLocal,True );
  169.       finally
  170.         Descompressor.ExtractZipFile( aIdFtp.DirectoryListing[i].FileName,aPastaDescompactacaoZip );
  171.       end;
  172.     end;      
  173.    Descompressor.DisposeOf;
  174.  
  175.     if assigned( FTDespoisdeExtrairArquivos ) then
  176.   begin
  177.     FTDespoisdeExtrairArquivos();
  178.   end;
  179. end;
  180.  
  181. procedure TAtttermEngineFtp.HandleException;
  182. begin
  183.  
  184.   FException := Exception(ExceptObject);
  185.   try
  186.     if not (FException is EAbort) then
  187.       Synchronize(DoHandleException);
  188.   finally
  189.     FException := nil;
  190.   end;
  191. end;
  192.  
  193. function TAtttermEngineFtp.PercorreArquivosDiretorio( const aDiretorioLocal,arqNameDirectoryFtp: String ): string;
  194.  var F: TSearchRec;
  195.      Ret: Integer;
  196.      var Age: integer;
  197. begin
  198.      Ret := FindFirst( aDiretorioLocal +arqNameDirectoryFtp, faAnyFile, F);
  199.      vArquiname := F.Name;
  200.      Age:= FileAge( vArquiname );
  201.      vArquiDateModifiqued := FileDateToDateTime( Age );
  202.      
  203.      Result:= vArquiname;
  204. end;
  205.  
  206. function TAtttermEngineFtp.PercorreListaFtp( aIdFtp: TIDFtp; const aPastaDesCompactacaoZip,aCamihoLocal,
  207.           aDiretorioFtp: string ): Boolean;
  208. var  i: integer;
  209.  begin
  210.     aIdFtp.ChangeDir( aDiretorioFtp );
  211.     aIdFtp.List;
  212.     for I := 0 to aIdFtp.DirectoryListing.Count -1 do
  213.     begin
  214.       if aIdFtp.DirectoryListing[i].FileName = PercorreArquivosDiretorio( aCamihoLocal,
  215.          aIdFtp.DirectoryListing[i].FileName ) then
  216.         begin
  217.          extractarquivosFtptoCaminhoTmp( aIdFtp,aPastaDesCompactacaoZip,aCamihoLocal,aDiretorioFtp  );
  218.          end;
  219.      
  220.     end;
  221.      
  222.  end;
  223.  
  224. end.
Add Comment
Please, Sign In to add comment