我有一个简单的形式,当一个.zip
文件被创建时,我正试图与ProgressBar一起工作,基于ZipDirectoryContents
回调。我在论坛上看到过一些Delphi的例子,但我不太了解它,我得到了几个错误。下面是一个例子:
. cpp
#include <system.zip.hpp>
String PreviousFilename;
bool __fastcall createZipFromFolder( String inZipName,
String inSourcePath,
String inTargetPath )
{
bool result = true;
sZipName = "C:\test\testBar.zip";
sSourceName = "C:\test2";
TZipFile* Zip = new TZipFile();
try{
if ( FileExists( sZipName ) ){
Zip->ZipDirectoryContents( sZipName, // destiny
sSourceName, // to include
zcDeflate, // compression mode
OnZipProgressEvent() ); // <--- p callback
Zip->Close();
}
}
catch(...){
result = false;
ShowMessage("Error");
}
delete Zip;
Zip = NULL;
return result;
}
void __fastcall OnZipProgressEvent( TObject* Sender, String FileName, TZipHeader Header, __int64 Position )
{
if ( PreviousFilename != FileName ){
PreviousFilename = FileName;
Form1->ProgressBar1->Max = Header.UncompressedSize;
Form1->ProgressBar1->Position = Position;
}
else{
Form1->ProgressBar1->Position = Position;
Application->ProcessMessages();
}
}
. h
#include <System.Zip.hpp>
public: // User declarations
__fastcall TForm1(TComponent* Owner);
typedef void __fastcall ( __closure * TZipProgressEvent )( TObject*,
String,
TZipHeader,
__int64 );
bool __fastcall createZipFromFolder( String inZipName,
String inSourcePath,
String inTargetPath );
void __fastcall OnZipProgressEvent( TObject* Sender,
String FileName,
TZipHeader Header,
__int64 Position );
};
extern PACKAGE TForm1 *Form1;
extern String PreviousFilename;
#endif
调用Zip->ZipDirectoryContents()
时,需要将OnZipProgressEvent()
改为&OnZipProgressEvent
。当您应该传递其地址时,您试图调用回调(缺少参数)。
同样,在你的.cpp
文件中,createZipFromFolder()
和OnZipProgressEvent()
都需要被定义为你的Form类的成员。
同样,在OnZipProgressEvent()
中,TZipHeader
参数需要通过const引用而不是通过值传递,根据文档。
试试这个:
Unit1.h
...
class TForm1 : public TForm
{
...
private: // User declarations
String PreviousFilename;
void __fastcall OnZipProgressEvent( TObject* Sender,
String FileName,
const TZipHeader &Header,
__int64 Position );
public: // User declarations
__fastcall TForm1(TComponent* Owner);
bool __fastcall createZipFromFolder( String inZipName,
String inSourcePath,
String inTargetPath );
};
extern PACKAGE TForm1 *Form1;
#endif
Unit1.cpp
...
#include "Unit1.h"
#include <System.Zip.hpp>
bool __fastcall TForm1::createZipFromFolder( String inZipName,
String inSourcePath,
String inTargetPath )
{
bool result = true;
sZipName = _D("C:\test\testBar.zip");
sSourceName = _D("C:\test2");
PreviousFilename = _D("");
TZipFile* Zip = new TZipFile();
try{
if ( FileExists( sZipName ) ){
Zip->ZipDirectoryContents( sZipName, // destiny
sSourceName, // to include
zcDeflate, // compression mode
&OnZipProgressEvent ); // <--- p callback
Zip->Close();
}
}
catch(...){
result = false;
ShowMessage(_D("Error"));
}
delete Zip;
Zip = NULL;
return result;
}
void __fastcall TForm1::OnZipProgressEvent( TObject* Sender,
String FileName,
const TZipHeader &Header,
__int64 Position )
{
if ( PreviousFilename != FileName ){
PreviousFilename = FileName;
ProgressBar1->Max = Header.UncompressedSize;
ProgressBar1->Position = Position;
}
else{
ProgressBar1->Position = Position;
Application->ProcessMessages();
}
}