Using Delphi Toyko creating a Zip file onprocess e

2019-08-20 16:36发布

问题:

I am trying to create a zip file with Delphi Tokyo using the command ZipDirectoryContents which has 4 parameters. They are

ZipDirectoryContents(const ZipFileName: string; const Path: string;
  Compression: TZipCompression = zcDeflate; 
  ZipProgress: TZipProgressEvent = nil); static;

Is there someone who can tell me how to use these parameters especially the TZipProgressEvent to show the progress of the zip file as it is adding the files from the directory. Thanks

回答1:

Here is the answer provided by Embarcadero

unit Unit16;

interface

uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls,System.Zip, Vcl.ComCtrls;

type
  TForm16 = class(TForm)
    Button1: TButton;
    ProgressBar1: TProgressBar;
    StaticText1: TStaticText;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
    PreviousFilename : string;
  public
    { Public declarations }

    procedure OnZipProgressEvent (Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64);
  end;

var
  Form16: TForm16;

implementation

{$R *.dfm}


procedure TForm16.Button1Click(Sender: TObject);
begin
    TZipFile.ZipDirectoryContents('C:\temp\Test.zip','c:\temp\zipTest',zcDeflate,OnZipProgressEvent);
end;

procedure TForm16.OnZipProgressEvent(Sender: TObject; FileName: string;
  Header: TZipHeader; Position: Int64);
begin
  if PreviousFilename <> FileName then
  begin
    StaticText1.Caption := ExtractFileName(FileName);
    PreviousFilename := FileName;
    ProgressBar1.Position := 0;
  end
  else
    ProgressBar1.Position := (Position * 100) div Header.UncompressedSize ;
  Application.ProcessMessages;
end;

end.