借鉴ZLibWrap.DLL进行二次加工生成DLL文件,在C++builder中进行动态调用DLL,压缩与解压标准ZIP文件操作!
因为有中文操作需要多出一个参数,所以要使用两份,本来准备写类,用构造函数,但发现有点大才小用,就使用COPY一份代码了!
DLL文件下载
http://download.csdn.net/detail/goodai007/4182207
DLL两个接口:
//压缩,bUtf8为false将支持路径与文件内包含中文
BOOL ZWZipCompress(LPCTSTR lpszSourceFiles, LPCTSTR lpszDestFile, bool bUtf8 = false);
//解压
BOOL ZWZipExtract(LPCTSTR lpszSourceFile, LPCTSTR lpszDestFolder);
bool __fastcall DoZipfile(String DoZip,String ZipFilename,String SourceFile,bool Check)
{
bool ZipReturn=false;
WideString w1;//必需要这样申请WideString变量,不然传值时会让两个变量使用同一样内存地址,搞了3个小时才发现这是BCB2006的BUG.
WideString w2;
LPCTSTR L1;//必需使用这个格式的变量,不然传过去到DLL时乱码。
LPCTSTR L2;
if(DoZip=="ZWZipCompress")//压缩
{
w1=SourceFile;
w2=ZipFilename;
L1=(const char*)w1.c_bstr();
L2=(const char*)w2.c_bstr();
bool __stdcall (*DllMethods)(LPCTSTR,LPCTSTR,bool);
HINSTANCE hInst=NULL;
hInst=LoadLibrary((ExtractFilePath(Application->ExeName)+"ZLibWrap.dll").c_str());//动态加载DLL
FARPROC P;
P = GetProcAddress(hInst,DoZip.c_str());
DllMethods=(bool __stdcall (__cdecl *)(LPCTSTR,LPCTSTR,bool))P;
if(DllMethods){
ZipReturn=DllMethods(L1,L2,Check);
}
FreeLibrary(hInst);
return ZipReturn;
}else if(DoZip=="ZWZipExtract")//解压
{
w1=ZipFilename;
w2=SourceFile;
L1=(const char*)w1.c_bstr();
L2=(const char*)w2.c_bstr();
bool __stdcall (*DllMethods)(LPCTSTR,LPCTSTR);
HINSTANCE hInst=NULL;
hInst=LoadLibrary((ExtractFilePath(Application->ExeName)+"ZLibWrap.dll").c_str());//当前目录下的DLL文件
FARPROC P;
P = GetProcAddress(hInst,DoZip.c_str());
DllMethods=(bool __stdcall (__cdecl *)(LPCTSTR,LPCTSTR))P;
if(DllMethods){
ZipReturn=DllMethods(L1,L2);
}
FreeLibrary(hInst);
return ZipReturn;
}
}
调用方法:
压缩目录:
bool Check=true;
if(CheckBox1->Enabled)
Check=false;//false支持中文
String DoZip="ZWZipCompress";
String ZipFilename=Edit1->Text;
String SourceFile=Edit2->Text;
if(DoZipfile(DoZip,ZipFilename,SourceFile,Check)){
ShowMessage("压缩成功!");
}else{
ShowMessage("压缩失败!");
}
解压目录:
bool Check=true;
if(CheckBox1->Enabled)
Check=false;//false支持中文
String DoZip="ZWZipExtract";
String ZipFilename=Edit1->Text;
String SourceFile=Edit2->Text;
if(DoZipfile(DoZip,ZipFilename,SourceFile,Check)){
ShowMessage("解压成功!");
}else{
ShowMessage("解压失败!");
}