前言
最近在使用解压各种格式文件,做进一步内容检测,发现docx是zip格式,doc是ole格式,zip格式可以用zlib库解压,ole却不行,发现7z可以解压,于是便有了一系列的操作。
步骤
bit7z 是一个开源项目,在7z的基础上做了一层封装,依赖7z的库,所以编译使用过程需要加入7z的DLL,和头文件等。
简单编译bit7z描述整个过程:
- 拉去bit7z代码
- 下载7z sdk
- 7z sdk解压放到到bit7z项目的third_part目录7-Zip
- 7z sdk解压再放到bit7z项目的include目录
- 下载7z sdk extra补充包 解压其中7za.dll 或者 7z.dll(根据自己的需求确认)
- 按照官网文档使用cmake构建和编译:
cd <bit7z folder>
mkdir build && cd build
cmake ../ -DCMAKE_BUILD_TYPE=Release
cmake --build . -j --config Release
注意:
7za.dll 只支持7z格式的解压和压缩
7z.dll 支持大多数格式的解压和压缩。
解压测试
注意:BitFormat::Auto 选项需要编译bit7z时,添加
BIT7Z_AUTO_FORMAT
宏
// This is an open source non-commercial project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
/*
* bit7z - A C++ static library to interface with the 7-zip shared libraries.
* Copyright (c) 2014-2022 Riccardo Ostani - All Rights Reserved.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
//#define BIT7Z_USE_NATIVE_STRING
#define BIT7Z_AUTO_FORMAT
#include <bitexception.hpp>
//#include <bitarchiveinfo.hpp>
#include <bit7zlibrary.hpp>
#include <bitarchivereader.hpp>
#include <bitfileextractor.hpp>
#include <iostream>
//#pragma comment(lib, "D:\\workspace\\github\\bit7z\\lib\\x64\\Release\\bit7z64.lib")
using namespace std;
using namespace bit7z;
int main(int argc, char* argv[]) {
try { // bit7z classes can throw BitException objects
using namespace bit7z;
Bit7zLibrary lib{ "D:\\workspace\\github\\bit7z\\bin\\x64\\Debug\\7z.dll" };
BitFileExtractor extractor{ lib, BitFormat::Auto };
// Extracting a simple archive
extractor.extract("D:\\test_office.doc", "D:\\test_office\\");
// Extracting a specific file
//extractor.extractMatching("path/to/archive.7z", "file.pdf", "out/dir/");
// Extracting the first file of an archive to a buffer
//std::vector< byte_t > buffer;
//extractor.extract("path/to/archive.7z", buffer);
// Extracting an encrypted archive
//extractor.setPassword("password");
//extractor.extract("path/to/another/archive.7z", "out/dir/");
}
catch (const bit7z::BitException& ex) {
printf(ex.what());
}
return 0;
}
总结
主要参考这个文章的:!!!C++ 7z解压缩编译及使用!!!
参考:
7z源码的编译与使用_markdown 格式
7z文件格式及其源码的分析(二)
笔记:7-zip在Visual Studio 2019下的编译
c++配置并使用bit7z加密压缩或解压7z文件
关于博主
wx/qq:binary-monster/1113673178
CSDN:https://blog.csdn.net/qq1113673178
码云:https://gitee.com/shiver
Github: https://github.com/ShiverZm
个人博客:www.shiver.fun