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&lt;void(double process)&gt; UnZipProcessCallback;
typedef std::function&lt;void(std::string filename)&gt; 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;
}

 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64

调用

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

 
 
  • 1
  • 2
  • 3

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

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-b6c3c6d139.css" rel="stylesheet">
                                <div class="more-toolbox">
            <div class="left-toolbox">
                <ul class="toolbox-list">
                    
                    <li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#csdnc-thumbsup"></use>
                    </svg><span class="name">点赞</span>
                    <span class="count"></span>
                    </a></li>
                    <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{&quot;mod&quot;:&quot;popu_824&quot;}"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-Collection-G"></use>
                    </svg><span class="name">收藏</span></a></li>
                    <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true">
                        <use xlink:href="#icon-csdnc-fenxiang"></use>
                    </svg>分享</a></li>
                    <!--打赏开始-->
                                            <!--打赏结束-->
                                            <li class="tool-item tool-more">
                        <a>
                        <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg>
                        </a>
                        <ul class="more-box">
                            <li class="item"><a class="article-report">文章举报</a></li>
                        </ul>
                    </li>
                                        </ul>
            </div>
                        </div>
        <div class="person-messagebox">
            <div class="left-message"><a href="https://blog.csdn.net/llmys">
                <img src="https://profile.csdnimg.cn/3/F/4/3_llmys" class="avatar_pic" username="llmys">
                                        <img src="https://g.csdnimg.cn/static/user-reg-year/1x/4.png" class="user-years">
                                </a></div>
            <div class="middle-message">
                                    <div class="title"><span class="tit"><a href="https://blog.csdn.net/llmys" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}" target="_blank">dofaster</a></span>
                                        </div>
                <div class="text"><span>发布了65 篇原创文章</span> · <span>获赞 19</span> · <span>访问量 11万+</span></div>
            </div>
                            <div class="right-message">
                                        <a href="https://im.csdn.net/im/main.html?userName=llmys" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信
                    </a>
                                                        <a class="btn btn-sm  bt-button personal-watch" data-report-click="{&quot;mod&quot;:&quot;popu_379&quot;}">关注</a>
                                </div>
                        </div>
                </div>
  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值