使用ZLIB生成DLL文件,并进行有中文的ZIP文件压缩与解压操作!支持目录压与解!

借鉴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("解压失败!");
	}


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值