Delphi 10.2如何使用ZipProgress

时间:2018-08-18 23:09:27

标签: delphi delphi-10.2-tokyo

一个较早的StackOverFlow主题提供了一些在使用TZipFile时使用progress事件的示例代码: Using Delphi Toyko creating a Zip file onprocess example

如果将示例用作主要形式,则能够使示例工作。但是我完全陷入了尝试将代码转换为在单独的单元中工作的麻烦。每次尝试将代码作为一个单元进行编译时,都会收到错误消息:E2009不兼容的类型:“方法指针和常规过程”。错误是在行上触发的:

TZipFile.ZipDirectoryContents('C:\ temp \ Test.zip','c:\ temp \ zipTest',zcDeflate,OnZipProgressEvent);

我已经以各种方式进行了研究,并尝试了所有其他方式,但没有运气。谁能解释如何使OnZipProgressEvent代码在单独的单元中工作?

这是我到目前为止的单位代码:

            unit ZipDelphiNative; 

            interface 

            uses 
            Windows,SysUtils, StrUtils, Variants, MaskUtils, Dialogs,StdCtrls, 
             Graphics, Menus,Classes, ClipBrd,  System.Zip, Winapi.Messages, 
             System.Generics.Collections, system.ioutils,  system.types, 
              Forms,ComCtrls, DateUtils; 

            Type 
            TZipProgressEvent = procedure(Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64) of object; 
            // TZipProgressEvent = procedure(Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64); 



              function jrCreateZipFromFolder(inZipName : string; inSourcePath : string; inTargetPath : string) : boolean; 
              procedure OnZipProgressEvent (Sender: TObject; FileName: string; Header: TZipHeader; Position: Int64); 

            var 
              PreviousFilename : string; 

            implementation 
            uses Unit1; 




            function jrCreateZipFromFolder(inZipName : string; inSourcePath : string; inTargetPath : string) : boolean; 
            //inTargetPath is already confirmed to be valid 
            //Zips all files in inSourcePath & subfolders 
            var 
              Zip : TZipFile; 
            //  ZipCallBack: TZipProgressEvent; 
              sZipName, sSourceName : string; 
            begin 
              result := true; 
            //  ZipCallBack :=  OnZipProgressEvent; 
              sZipName := IncludeTrailingBackslash(inTargetPath) + inZipName; 
              sSourceName := IncludeTrailingBackslash(inSourcePath) ; 
              Zip := TZipFile.Create; 
              try 
                if FileExists(sZipName) then 
                  DeleteFile(sZipName); 

            //    zip.ZipDirectoryContents(sZipName, sSourceName, zcDeflate, ZipCallBack);  //do not store folder too 

                zip.ZipDirectoryContents(sZipName, sSourceName, zcDeflate, OnZipProgressEvent);  //do not store folder too 


                zip.Close; 

              except 
                result := false; 

              end; 
              FreeAndNil(Zip); 
            end; 


            procedure OnZipProgressEvent(Sender: TObject; FileName: string; 
              Header: TZipHeader; Position: Int64); 
            begin 
              if PreviousFilename <> FileName then 
              begin 
                PreviousFilename := FileName; 
                unit1.Form1.ProgressBar1.Max := Header.UncompressedSize; 
                unit1.Form1.ProgressBar1.Position := Position; 

              end 
              else 
            //    Unit1.Form1.ProgressBar1.Position := (Position * 100) div Header.UncompressedSize ; 
                Unit1.Form1.ProgressBar1.Position := Position; 
              Application.ProcessMessages; 
            end; 




            end. 

我尝试定义带和不带“对象”的TZipProgressEvent。在此代码中,我有两个使用TZipProgressEvent的选项,例如原始示例或定义ZipCallBack的注释版本。当以某种形式使用时,两者都在不同的情况下工作。在本机中使用时,两者均无效。

所以我对如何使这项工作感到困惑。如果Embarcadero提供有用的示例代码肯定会很好!

1 个答案:

答案 0 :(得分:1)

// delegate method func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { let cell = tableView.dequeueReusableCell(withIdentifier: "reuse id", for: indexPath) as! YourCustomCell cell.contentView.backgroundColor = cellColor // anything else... return cell } 被声明为TZipProgressEvent,这意味着它必须是对象(类)的方法。因此,为了实现它,您需要声明一个使用该签名(of object类中的签名,而不是您所组成的签名)来实现该方法的类。

这应该使您入门。正如我在下面的代码注释中所述,将您自己的代码添加到TZipjrCreateZipFromFolder的实现中。我将其设置为TMyZipProgress.ShowZipProgress,以便您不必创建class procedure的实例即可使用它。

TMyZipProgress