news 2026/5/15 3:55:04

bit7z压缩与解压

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
bit7z压缩与解压

bit7z是一个对7-zip的静态wrapper库(GitHub - rikyoz/bit7z: A C++ static library offering a clean and simple interface to the 7-zip shared libraries. · GitHub),大大简化了文件压缩与解压的过程,提供一个简单的示例。7-zip支持市面上绝大多数压缩文件格式(zip,7z,tar,xz,...),都可以通过bit7z进行调用。

#pragma once #include <bit7z/bitarchivereader.hpp> #include <bit7z/bitarchivewriter.hpp> #include <io.h> #include <cstdio> typedef std::function<void(double progress)> ProgressCallback; typedef std::function<void(bit7z::tstring filename)> MyFileCallback; typedef std::function<void(const bit7z::BitException&)> BitExceptionCallback; template<class T> class FZHelper { public: FZHelper() :TotalSize(0), m_pProgressFunc(nullptr), m_pFileFunc(nullptr), m_pExpFunc(nullptr) {} inline void TotalSizeNotify(uint64_t total_size) { return static_cast<T*>(this)->TotalSizeCB(total_size); } inline bool ProgressNotify(uint64_t size) { return static_cast<T*>(this)->ProgressCB(size); } inline void FileNameNotify(bit7z::tstring filename) { return static_cast<T*>(this)->FileNameCB(filename); } inline void TotalSizeCB(uint64_t total_size) { TotalSize = total_size; printf("TotalCallback: %lld.\n", total_size); } inline bool ProgressCB(uint64_t size) { double progress = ((1.0 * size) / TotalSize); printf("ProgressCallback: %.4f.\n", TotalSize == 0 ? 0.0 : (100 * 1.0 * size) / TotalSize); if (m_pProgressFunc) { m_pProgressFunc(progress); } return true; } inline void FileNameCB(bit7z::tstring filename) { printf("Begin Process File %s.\n", filename.c_str()); if (m_pFileFunc) { m_pFileFunc(filename); } } protected: uint64_t TotalSize; private: ProgressCallback m_pProgressFunc; MyFileCallback m_pFileFunc; BitExceptionCallback m_pExpFunc; }; template <typename T> bool ExtractFileTo(const char* zipSrcPath, const char* dstDir, const bit7z::BitInOutFormat& bitFormat, FZHelper<T>* hlp) { bool bRet = false; DWORD attr = 0; if (_access(dstDir, 0) == -1) return bRet; if (_access(zipSrcPath, 0) == -1) return bRet; attr = GetFileAttributesA(dstDir); if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) return bRet; try { // bit7z classes can throw BitException objects using namespace bit7z; Bit7zLibrary lib{ "7z.dll" };//find 7z.dll on your computer,if you don't have 7z.dll, then install 7z software to get one. // Opening the archive BitArchiveReader archive{ lib, zipSrcPath, BitFormat::SevenZip }; if (hlp != nullptr) { bit7z::TotalCallback tcb = [hlp](uint64_t sz) {hlp->TotalSizeNotify(sz); }; bit7z::FileCallback fcb = [hlp](bit7z::tstring filename) {hlp->FileNameNotify(filename); }; bit7z::ProgressCallback pcb = [hlp](uint64_t sz) {return hlp->ProgressNotify(sz); }; archive.setTotalCallback(tcb); archive.setFileCallback(fcb); archive.setProgressCallback(pcb); } //archive.setPassword("123456"); // Testing the archive archive.test(); // Extracting the archive archive.extractTo(dstDir); bRet = true; } catch (const bit7z::BitException& ex) { std::error_code code = ex.code(); int errCode = code.value(); if (errCode == 126) printf("Please Get the 7z mudule firstly.\n"); printf("error: %s[%d].\n", ex.what(), errCode); } return bRet; } template <typename T> bool CompressFolderTo(const char* folderPath, const char* zipDstPath, const bit7z::BitInOutFormat& bitFormat, FZHelper<T>* hlp) { bool bRet = false; try { // bit7z classes can throw BitException objects using namespace bit7z; Bit7zLibrary lib{ "7z.dll" };//find 7z.dll on your computer,if you don't have 7z.dll, then install 7z software to get one. BitArchiveWriter archive{ lib, BitFormat::SevenZip }; if (hlp != nullptr) { bit7z::TotalCallback tcb = [hlp](uint64_t sz) {hlp->TotalSizeNotify(sz); }; bit7z::FileCallback fcb = [hlp](bit7z::tstring filename) {hlp->FileNameNotify(filename); }; bit7z::ProgressCallback pcb = [hlp](uint64_t sz) {return hlp->ProgressNotify(sz); }; archive.setTotalCallback(tcb); archive.setFileCallback(fcb); archive.setProgressCallback(pcb); } //archive.setPassword("123456"); // Adding the items to be compressed (no compression is performed here) //archive.addFile("C:/Users/project/test/file1.txt", "A/B/file1.txt"); //archive.addFile("C:/Users/project/test/file2.txt", "A/file2.txt"); archive.addDirectory(folderPath);//add the whole test folder(include the all subfolders and files of test) if (_access(zipDstPath, 0) == 0)//delete if already existed. { if (remove(zipDstPath) != 0) { goto end; } } // Compressing the added items to the output archive archive.compressTo(zipDstPath); bRet = true; } catch (const bit7z::BitException& ex) { std::error_code code = ex.code(); int errCode = code.value(); if (errCode == 126) printf("Please Get the 7z mudule firstly.\n"); printf("error: %s[%d].\n", ex.what(), errCode); } end: return bRet; }
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/5/15 3:54:19

openclaw视频剪辑命令行工具推荐,小龙虾自动化批处理功能解析

对于运营团队、电商操盘手或矩阵号管理者来说&#xff0c;每天要处理几十甚至上百条视频&#xff1a;加字幕、剪气口、去重、配乐、提取文案、生成数字人……如果每一条都手动操作&#xff0c;不仅效率低下&#xff0c;还容易出错。因此&#xff0c;“有没有办法用命令行批量处…

作者头像 李华
网站建设 2026/5/15 3:51:13

高性能计算能效优化:从异构架构到混合精度实践

1. 高性能计算能效优化的核心挑战在过去的十年里&#xff0c;高性能计算&#xff08;HPC&#xff09;系统的能耗问题已经从单纯的运营成本问题演变为制约科学发现速度的关键瓶颈。以欧洲核子研究中心&#xff08;CERN&#xff09;的大型强子对撞机&#xff08;LHC&#xff09;为…

作者头像 李华
网站建设 2026/5/15 3:50:43

开源停车数据聚合工具:从爬虫到API的完整架构与实现

1. 项目概述&#xff1a;一个开源停车查询工具的诞生最近在GitHub上看到一个挺有意思的项目&#xff0c;叫openclaw-parking-query。光看名字&#xff0c;你大概能猜到这是个跟停车查询相关的工具&#xff0c;但“openclaw”这个前缀又带点神秘感。作为一个经常在各大城市开车、…

作者头像 李华
网站建设 2026/5/15 3:44:03

手摸手教你用Claude多智能体,零代码构建专属“超级办公助理”全过程

引言 2026年&#xff0c;多智能体技术已经从概念全面落地到日常办公场景。对于程序员和IT从业者来说&#xff0c;每天要处理大量重复的文档整理、数据统计、邮件回复和任务规划工作。单个AI虽然能解决部分零散问题&#xff0c;但遇到跨环节的复杂任务时&#xff0c;经常出现逻辑…

作者头像 李华
网站建设 2026/5/15 3:38:06

携程问道(workbuddy 合作版)技能接入与使用文档

本文档详细介绍携程问道&#xff08;workbuddy 合作版&#xff09;技能&#xff08;wendao-partner-workbuddy-skill&#xff09;的接入流程、使用方法、环境配置及注意事项&#xff0c;适用于需要集成该技能并调用携程问道 API 获取旅行相关信息的开发 / 运维人员。一、技能概…

作者头像 李华
网站建设 2026/5/15 3:31:28

大语言模型思维树框架:从链式推理到多路径搜索的工程实践

1. 项目概述&#xff1a;当大模型学会“三思而后行”最近在探索如何让大语言模型&#xff08;LLM&#xff09;的推理能力再上一个台阶时&#xff0c;我深度体验了kyegomez/tree-of-thoughts这个项目。简单来说&#xff0c;它不是一个具体的应用&#xff0c;而是一个思维框架的实…

作者头像 李华