C++解压库bit7z编译以及使用

1.编译bit7z库

bit7z是一个C++静态库,其封装了简单易用的接口,用于调用7-zip库;编译该库,首先需要下载以下源码:

解压bit7z、lzma;在编译之前,需要将lzma解压出来的文件放在bit7z的lib\7zSDK目录下,如下图所示:
在这里插入图片描述接下来就可以使用VS打开bit7z.vcxproj进行编译了,编译结果会产生两个静态库bit7z.lib、bit7z_d.lib,此时我们就可以在程序用调用该库了。由于bit7z依赖7z库,所以还需要下载7z的dll方可正常运行

2.使用示例

封装ZipHelper类如下:
头文件

#pragma once
#ifndef NIMO_OBS_ZLIB_H
#define NIMO_OBS_ZLIB_H
#include <iostream>
#include <string>
#include <functional>
#include "bit7z.hpp"
#include "bit7zlibrary.hpp"

class ZlibHelper {
private:
	std::string msSourcePath;
	std::string msDestDir;
	uint64_t mSize;

	typedef std::function<void(double process)> UnZipProcessCallback;
	typedef std::function<void(std::string filename)> UnZipFileCallback;
	UnZipProcessCallback upc;
	UnZipFileCallback ufc;
public:
	ZlibHelper(std::string src, std::string dest); 
	~ZlibHelper();

	void Extract();											// 解压
	void SetUnZipProcessCallback(UnZipProcessCallback upc);
	void SetUnZipFileCallback(UnZipFileCallback ufc);
private:
	void GetSizeOfZipPackage();
	void ProcessCallback(uint64_t size);
	void FileCallback(std::wstring filename);
};
#endif

源文件

#include "Zlib.h"
//----------------------------------------------------------------------------
ZlibHelper::ZlibHelper(std::string src, std::string dest) : 
	upc(nullptr), ufc(nullptr)
{
	msSourcePath = src;
	msDestDir = dest;
	GetSizeOfZipPackage();
}
//----------------------------------------------------------------------------
ZlibHelper::~ZlibHelper()
{
}
//----------------------------------------------------------------------------
// 获取Zip包的大小
void ZlibHelper::GetSizeOfZipPackage()
{
	std::wstring src = StringUtil::StringToWString(msSourcePath.c_str());
	bit7z::Bit7zLibrary lib(L"7z.dll");
	bit7z::BitArchiveInfo info(lib, src, bit7z::BitFormat::Zip);
	mSize = info.size();
}
//----------------------------------------------------------------------------
void ZlibHelper::ProcessCallback(uint64_t size)
{
	double process = ((1.0 * size) / mSize);
	//std::wcout << process << "%" << std::endl;
	if (upc) {
		upc(process);
	}
}
//----------------------------------------------------------------------------
void ZlibHelper::FileCallback(std::wstring filename)
{
	std::string temp = StringUtil::WStringToString(filename.c_str());
	//std::cout << temp.c_str() << std::endl;
	if (ufc) {
		ufc(temp);
	}
}
//----------------------------------------------------------------------------
// 解压
void ZlibHelper::Extract()
{
	bit7z::Bit7zLibrary lib(L"7z.dll");
	bit7z::BitExtractor extractor(lib, bit7z::BitFormat::Zip);
	bit7z::ProgressCallback pc = std::bind(&ZlibHelper::ProcessCallback, this, std::placeholders::_1);
	bit7z::FileCallback fc = std::bind(&ZlibHelper::FileCallback, this, std::placeholders::_1);
	extractor.setProgressCallback(pc);
	extractor.setFileCallback(fc);
	std::wstring src = StringUtil::StringToWString(msSourcePath.c_str());
	std::wstring dest = StringUtil::StringToWString(msDestDir.c_str());
	extractor.extract(src, dest);
}
//----------------------------------------------------------------------------
void ZlibHelper::SetUnZipProcessCallback(UnZipProcessCallback upc)
{
	this->upc = upc;
}
//----------------------------------------------------------------------------
void ZlibHelper::SetUnZipFileCallback(UnZipFileCallback ufc)
{
	this->ufc = ufc;
}

调用

ZlibHelper* pUnzip = new ZlibHelper("2.zip", "output");
pUnzip->SetUnZipProcessCallback(std::bind(&CustomInstalWndViewCtrl::ProcessCallback, this, std::placeholders::_1));
pUnZip->Extract();

调用可以输出解压文件信息和百分比:
在这里插入图片描述

  • 1
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值