本文使用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
需要的文件
下载文件
- 进入7-Zip下载Extra文件
- 进入Bit7z v3.1.2下载bit7z-v3.1.2-msvc2019_x64.7z文件
创建项目
- 在vs2019中创建空项目
配置dll与lib
- 打开项目目录,将文件7z1900-extra.7z解压并将x64下的7zxa.dll与7za.dll与文件bit7z-v3.1.2-msvc2019_x64.7z中的lib与include文件夹解压到项目根目录。
将以上选择的文件复制到项目根目录,此时项目目录如下
- 配置vs2019,将解决方案平台设置为x64,将头文件、lib导入vs2019
将lib加入附加库目录
在输入中附加依赖项键入bit7z64.lib与bit7z64_d.lib
最后再添加到命令行
此添加lib方式参考
https://www.jianshu.com/p/43f889a2b91e
测试运行
- 在项目中创建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
运行后出现以下界面则成功
加密压缩
复制以下代码到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++ -> 常规 -> 附加包含目录,则不用将头全部添加到项目头文件中