Advertisement
filhotecmail

Compressão de Arquivos

Aug 17th, 2017
318
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 2.67 KB | None | 0 0
  1. unit CompressaoArquivos;
  2.  
  3. interface
  4.  
  5. uses Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls, ComCtrls, ACBrMail, types, ACBrBase, ExtCtrls,
  6.       Vcl.Imaging.pngimage, System.Zip,JvZlibMultiple, System.Zip2;
  7.  
  8. type ICompressao= Interface
  9.       ['{79A8CCDC-2F8C-48CD-8B5F-5146876DBC4B}']
  10.       function GetTamanhoArquivos(Path: string): integer;
  11.       procedure EventoOnProgress(Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64);
  12. End;
  13.  
  14. Type TCompressao = class(TInterfacedObject, ICompressao)
  15.      private
  16.     FZipFile: TZipFile;
  17.     FrPgrBar: TProgressbar;
  18.     {Private declaration}
  19.      protected
  20.      {Protected declaration}
  21.      public
  22.      {Public declaration declaration}
  23.      property ZipFile: TZipFile read FZipFile write FZipFile;
  24.      procedure EventoOnProgress(Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64);
  25.      property  PgrBar : TProgressbar read FrPgrBar write FrPgrBar;
  26.      constructor Create(aNomearquivoZip,arquivo: string;aTPrgBar: TprogressBar); (*Metodos do Constructor*)
  27.      function GetTamanhoArquivos(Path: string): integer;
  28.      procedure BeforeDestruction; override;
  29.      destructor Destroy; override;
  30.  
  31.      published
  32.      {Protected declaration}
  33.      end;
  34.  
  35. implementation
  36.  
  37. { TCompressao }
  38.  
  39. procedure TCompressao.BeforeDestruction;
  40. begin
  41.   inherited BeforeDestruction;
  42.  
  43. end;
  44.  
  45. constructor TCompressao.Create(aNomearquivoZip,arquivo: string;aTPrgBar: TprogressBar);
  46.   var ZipProgress: TZipProgressEvent;
  47. begin
  48.     ZipFile:= TZipFile.Create;
  49.   try
  50.     FrPgrBar := Tprogressbar.Create(Nil);
  51.     ZipFile.OnProgress := EventoOnProgress;
  52.     self.FrPgrBar:= aTPrgBar;
  53.     FrPgrBar.Max := 1;
  54.     ZipFile.ZipDirectoryContents(aNomearquivoZip,arquivo);
  55.  
  56.   finally
  57.     ZipFile.DisposeOf;
  58.  
  59.     //FrPgrBar.DisposeOf;
  60.   end;
  61. end;
  62.  
  63. destructor TCompressao.Destroy;
  64. begin
  65.  
  66.   inherited;
  67. end;
  68.  
  69. procedure TCompressao.EventoOnProgress(Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64);
  70. begin
  71.  Application.ProcessMessages;
  72.  FrPgrBar.Position := trunc(Position / Header.UncompressedSize * 100);
  73.  
  74. end;
  75.  
  76. function TCompressao.GetTamanhoArquivos(Path: string): integer;
  77. {Retorna o tamanho de um diretório}
  78. var
  79. SR : TSearchRec;
  80. rc : Integer;
  81. Tamanho : Integer;
  82. begin
  83. Tamanho := 0;
  84. rc := FindFirst(Path+'\*.*', faDirectory, SR);
  85. if rc = 0 then
  86. begin
  87. while rc = 0 do
  88. begin
  89. if (SR.Name = '.') or (SR.Name = '..') then
  90. else
  91. if (SR.Attr and faDirectory) <> 0 then
  92. begin
  93. Tamanho := Tamanho + GetTamanhoArquivos(Path+SR.Name);
  94. end
  95. else
  96. begin
  97. Tamanho := Tamanho + SR.Size;
  98. end;
  99. rc := FindNext(SR);
  100. end;
  101. FindClose(SR);
  102. end;
  103. result := Tamanho;
  104. end;
  105.  
  106. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement