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

1.编译bit7z库

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

  • 下载bit7z:https://github.com/rikyoz/bit7z
  • 下载lzma:https://jaist.dl.sourceforge.net/project/sevenzip/LZMA SDK/lzma1805.7z

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

  • 7za.dll:下载链接
  • 7z.dll:下载链接
2.使用示例

封装ZipHelper类如下:
头文件

#pragma once
#ifndef NIMO_OBS_ZLIB_H
#define NIMO_OBS_ZLIB_H
#include 
#include 
#include 
#include "bit7z.hpp"
#include "bit7zlibrary.hpp"

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

	typedef std::function UnZipProcessCallback;
	typedef std::function 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();

调用可以输出解压文件信息和百分比:
C++解压库bit7z编译以及使用_第2张图片

你可能感兴趣的:(c/c++,C++,7z,bit7z,解压,压缩)