c++配置并使用bit7z加密压缩或解压7z文件

本文使用bit7z

bit7z is a C++ static library which allows to compress and extract many file archive formats, all through a clean, simple and entirely object-oriented interface to the dynamic libraries from the 7-zip project (https://www.7-zip.org/).
It supports compression and extraction to and from the filesystem or the memory, reading of archives metadata, updating existing archives, creation of multi-volume archives, operation progress callbacks and many other functionalities.

开发环境

  • VS2019
  • windows10

需要的文件

下载文件

  1. 进入7-Zip下载Extra文件
    7-Zip下载
  2. 进入Bit7z v3.1.2下载bit7z-v3.1.2-msvc2019_x64.7z文件
    Bit7z v3.1.2下载

创建项目

  1. 在vs2019中创建空项目
    创建空项目
    配置新项目

配置dll与lib

  1. 打开项目目录,将文件7z1900-extra.7z解压并将x64下的7zxa.dll与7za.dll与文件bit7z-v3.1.2-msvc2019_x64.7z中的lib与include文件夹解压到项目根目录。
    解压7z1900-extra.7z
    解压bit7z
    将以上选择的文件复制到项目根目录,此时项目目录如下
    项目目录
  2. 配置vs2019,将解决方案平台设置为x64,将头文件、lib导入vs2019
    将lib加入附加库目录
    附加库目录
    在输入中附加依赖项键入bit7z64.lib与bit7z64_d.lib
    附加依赖项
    最后再添加到命令行
    配置命令行此添加lib方式参考

https://www.jianshu.com/p/43f889a2b91e

测试运行

  1. 在项目中创建main.cpp
    键入以下代码:
#include "include/bitexception.hpp"
#include "include/bitarchiveinfo.hpp"
#include <iostream>

using namespace std;
using namespace bit7z;

int main() {
    try {
        Bit7zLibrary lib{ L"7za.dll" };
        // 自己的7z文件路径
        BitArchiveInfo arc{ lib, L"C:\\Users\\monoliths\\Downloads\\edge\\7z1805-extra.7z", BitFormat::SevenZip };

        //printing archive metadata
        wcout << L"Archive properties" << endl;
        wcout << L" Items count: " << arc.itemsCount() << endl;
        wcout << L" Folders count: " << arc.foldersCount() << endl;
        wcout << L" Files count: " << arc.filesCount() << endl;
        wcout << L" Size: " << arc.size() << endl;
        wcout << L" Packed size: " << arc.packSize() << endl;
        wcout << endl;

        //printing archive items metadata
        wcout << L"Archive items";
        auto arc_items = arc.items();
        for (auto& item : arc_items) {
            cout << endl;
            wcout << L" Item index: " << item.index() << endl;
            wcout << L"  Name: " << item.name() << endl;
            wcout << L"  Extension: " << item.extension() << endl;
            wcout << L"  Path: " << item.path() << endl;
            wcout << L"  IsDir: " << item.isDir() << endl;
            wcout << L"  Size: " << item.size() << endl;
            wcout << L"  Packed size: " << item.packSize() << endl;
        }
    }
    catch (const BitException& ex) {
    }
    return 0;
}

引自bit7z README.md

https://github.com/rikyoz/bit7z#reading-archive-metadata

运行后出现以下界面则成功
列出7z内文件

加密压缩

复制以下代码到main

#include "include/bitcompressor.hpp"
#include "include/bitexception.hpp"
#include <iostream>

using namespace std;
using namespace bit7z;

int main() {

    try {
        Bit7zLibrary lib{ L"7za.dll" };
        BitCompressor compressor{ lib, BitFormat::SevenZip };

        compressor.compressDirectory(L"C:\\Users\\monoliths\\Videos\\Captures\\jni", L"C:\\Users\\monoliths\\Videos\\Captures\\jni.7z"); //compressing a directory

        compressor.setPassword(L"password");
        compressor.setUpdateMode(true);
    }
    catch (const BitException& ex) {
        //do something with ex.what()...
        cout << ex.what() << endl;
    }
	return 0;
}

压缩后的文件

解压

#include "include/bitextractor.hpp"
#include "include/bitexception.hpp"
#include <iostream>

using namespace std;
using namespace bit7z;

int main() {
    try {
        Bit7zLibrary lib{ L"7za.dll" };
        BitExtractor extractor{ lib, BitFormat::SevenZip };
        //extracting an encrypted archive
        extractor.setPassword(L"password");
        extractor.extract(L"C:\\Users\\monoliths\\Videos\\Captures\\jni.7z", L"C:\\Users\\monoliths\\Videos\\Captures\\jni"); //extracting a simple archive    }
    }
    catch (const BitException& ex) {
        //do something with ex.what()...
        cout << ex.what() << endl;
    }
    return 0;
}

解压缩
更多示例
有关更多使用方法请自行查阅文档

以上便是c++配置并使用bit7z加密压缩或解压7z文件的全文了

本文为博主原创,如需转载,请注明出处,感谢阅读。

修正:

  • 可以将include目录添加到C++ -> 常规 -> 附加包含目录,则不用将头全部添加到项目头文件中
  • 7
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 15
    评论
好的,您的问题是如何使用 bit7z 库在 MFC 中解压 7z 文件并显示进度条。下面是一个简单的示例: 1. 首先,您需要下载并安装 bit7z 库,可以从以下链接中获取:https://github.com/bitbeans/bit7z 2. 在您的 MFC 项目中,添加 Bit7z.hpp 头文件,并使用以下代码创建一个解压缩进度条对话框类: ```c++ class CProgressDlg : public CDialogEx { public: CProgressDlg(CWnd* pParent = nullptr); enum { IDD = IDD_PROGRESS_DIALOG }; protected: virtual void DoDataExchange(CDataExchange* pDX); DECLARE_MESSAGE_MAP() private: CProgressCtrl m_ProgressCtrl; CString m_strFileName; public: void SetFileName(CString strFileName); void SetProgress(int nPercent); }; ``` 3. 在您的 MFC 应用程序的主窗口类中,添加以下代码以启动解压缩进度条对话框: ```c++ void CMyApp::OnUnzip7z() { // 创建解压缩进度条对话框类的实例 CProgressDlg dlgProgress(this); // 获取要解压缩的文件名 CString strFileName = "your_7z_file.7z"; // 设置解压缩进度条对话框的文件名 dlgProgress.SetFileName(strFileName); // 显示解压缩进度条对话框 dlgProgress.DoModal(); // 在后台线程中解压文件 std::thread th([=, &dlgProgress]() { // 创建 Bit7z::InArchive 对象 Bit7z::InArchive archive(Bit7z::InArchive::OpenFile(strFileName)); // 获取要解压缩的文件列表 std::vector<Bit7z::InArchive::File> files = archive.GetFiles(); // 计算解压缩总字节数 uint64_t nTotalBytes = 0; for (auto& file : files) nTotalBytes += file.Size; // 解压缩每个文件 uint64_t nCurrentBytes = 0; for (auto& file : files) { // 创建要解压缩的文件在磁盘上的路径 CString strFilePath = "your_extract_path\\" + CString(file.Name); // 创建要解压缩的文件在内存中的缓冲区 std::vector<uint8_t> buffer(file.Size); // 读取要解压缩的文件到缓冲区中 archive.ReadData(file, buffer.data(), buffer.size()); // 将缓冲区中的数据写入到磁盘上的文件中 FILE* fp = nullptr; fopen_s(&fp, strFilePath, "wb"); fwrite(buffer.data(), buffer.size(), 1, fp); fclose(fp); // 更新解压缩进度条 nCurrentBytes += file.Size; int nPercent = (int)(nCurrentBytes * 100 / nTotalBytes); dlgProgress.SetProgress(nPercent); } // 关闭解压缩进度条对话框 dlgProgress.EndDialog(IDOK); }); th.detach(); } ``` 4. 最后,您需要在您的 MFC 应用程序中添加 bit7z 库的链接器附加项。在 Visual Studio 中,可以通过右键单击您的项目,选择“属性”,然后选择“链接器”->“输入”->“附加依赖项”来完成此操作。 希望这可以帮助您解决问题!
评论 15
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ALoppd

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值