当前位置: 首页 > news >正文

使用 C++ 和 minizip 实现 ZIP 压缩解压工具

在软件开发中,文件压缩和解压是常见的需求。今天我们来介绍一个基于 minizip 库的 C++ ZIP 工具类 - ZipUtility,它可以轻松地处理 ZIP 文件的创建和解压。

这篇文章使用AI辅助编写。

核心功能

1. 压缩功能

ZipUtility::zipArchive 方法可以将多个文件打包成一个 ZIP 压缩包:

std::vector<std::string> files = {"document.txt", "image.jpg"};
ZipUtility::zipArchive(files, "archive.zip");

支持的特性:

  • 多种压缩方法:存储(不压缩)、DEFLATE、BZIP2
  • 压缩级别控制:从最快速度到最佳压缩比
  • 密码保护:使用密码加密压缩文件
  • ZIP64 支持:处理大文件(超过 4GB)
  • 全局注释:为 ZIP 文件添加描述信息

2. 解压功能

ZipUtility::unzipArchive 方法可以解压 ZIP 文件:

ZipUtility::unzipArchive("archive.zip", "output_folder");

支持的特性:

  • 目录结构恢复:可选择是否保留原始目录结构
  • 密码解压:支持加密 ZIP 文件的解压
  • 自动目录创建:递归创建所需的目录结构

设计亮点

跨平台兼容

代码通过条件编译实现了 Windows 和 Unix-like 系统的兼容:

#ifdef _WIN32#define SEPARATOR '\\'
#else#define SEPARATOR '/'
#endif

完善的错误处理

定义了详细的错误码枚举,涵盖各种可能的问题场景:

enum class ZipResult
{OK,                    // 操作成功ERR_OPEN_ZIP,          // 打开ZIP文件失败ERR_CREATE_ZIP,        // 创建ZIP文件失败ERR_OPEN_FILE,         // 打开源文件失败// ... 更多错误码
};

递归目录创建

createDirectory 方法能够递归创建多级目录,确保解压路径的正确性。

使用示例

基本压缩

std::vector<std::string> files = {"file1.txt", "file2.jpg"};
auto result = ZipUtility::zipArchive(files, "backup.zip",ZipUtility::CompressionMethod::DEFLATE,ZipUtility::CompressionLevel::BEST_COMPRESSION
);

加密压缩

auto result = ZipUtility::zipArchive(files, "secure.zip",ZipUtility::CompressionMethod::DEFLATE,ZipUtility::CompressionLevel::DEFAULT,"mypassword"  // 设置密码
);

解压文件

// 保留目录结构
auto result = ZipUtility::unzipArchive("archive.zip", "extracted", nullptr, true);// 仅提取文件(不保留目录结构)
auto result = ZipUtility::unzipArchive("archive.zip", "extracted", nullptr, false);

总结

这个 ZipUtility 类提供了一个简洁而强大的接口来处理 ZIP 文件操作。它的设计考虑了实际使用中的各种需求,包括跨平台兼容性、错误处理和功能灵活性。无论是用于文件备份、数据分发还是应用程序的资源管理,都是一个很好的工具选择。

代码结构清晰,错误处理完善,非常适合集成到现有的 C++ 项目中。如果你正在寻找一个轻量级的 ZIP 处理解决方案,这个工具类值得一试!

完整代码

ZipUtility.h

#include <string>
#include <vector>class ZipUtility
{
public:// 操作结果枚举enum class ZipResult{OK,                     // 操作成功ERR_OPEN_ZIP,           // 打开ZIP文件失败ERR_CREATE_ZIP,         // 创建ZIP文件失败ERR_OPEN_FILE,          // 打开源文件失败ERR_READ_FILE,          // 读取文件失败ERR_WRITE_FILE,         // 写入文件失败ERR_CLOSE_ZIP,          // 关闭ZIP文件失败ERR_CREATE_DIR,         // 创建目录失败ERR_INVALID_ZIP,        // 无效的ZIP文件ERR_INVALID_PASSWORD,   // 密码错误ERR_CRC,                // CRC校验失败ERR_UNSUPPORTED,        // 不支持的格式ERR_UNKNOWN             // 未知错误};// 压缩方法枚举enum class CompressionMethod{STORE   = 0,   // 不压缩DEFLATE = 8,   // Z_DEFLATED DEFLATE压缩BZIP2   = 12   // Z_BZIP2ED BZIP2压缩(如果编译支持)};// 压缩级别枚举enum class CompressionLevel{DEFAULT          = -1,   // Z_DEFAULT_COMPRESSION,BEST_SPEED       = 1,    // Z_BEST_SPEED,BEST_COMPRESSION = 9,    // Z_BEST_COMPRESSION,NO_COMPRESSION   = 0     // Z_NO_COMPRESSION};/*** 创建 ZIP 压缩文件** @param filelist       需要压缩的文件路径列表* @param zipPath        输出的 ZIP 文件路径* @param method         压缩方法 (默认为DEFLATE)* @param level          压缩级别 (默认为DEFAULT)* @param password       加密密码 (默认为空)* @param zip64          是否启用ZIP64格式(大文件支持) (默认为true)* @param globalComment  ZIP文件的全局注释 (默认为空)* @return               操作状态*/static ZipResult zipArchive(const std::vector<std::string>& filelist,const std::string&              zipPath,CompressionMethod               method = CompressionMethod::DEFLATE,CompressionLevel                level  = CompressionLevel::DEFAULT,const char* password = nullptr, bool zip64 = true,const char* globalComment = nullptr);/*** 解压 ZIP 文件** @param zipPath        输入的 ZIP 文件路径* @param outDirectory   解压输出目录* @param password       解压密码 (默认为空)* @param restorePath    是否保留目录结构 (默认为true)* @return               操作状态*/static ZipResult unzipArchive(const std::string& zipPath, const std::string& outDirectory,const char* password = nullptr, bool restorePath = true);private:// 创建目录(递归)static ZipResult createDirectory(const std::string& path);// 转换ZIP操作错误码static ZipResult convertZipError(int err);// 转换UNZIP操作错误码static ZipResult convertUnzipError(int err);
};

ZipUtility.cpp

#include "ZipUtility.h"#include "minizip/ioapi.h"
#include "minizip/unzip.h"
#include "minizip/zip.h"#include <algorithm>
#include <cstring>
#include <ctime>
#include <string>
#include <sys/stat.h>
#include <vector>#ifdef _WIN32
#    include <direct.h>
#    include <windows.h>
#    define mkdir(path, mode) _mkdir(path)
#    define SEPARATOR '\\'
#else
#    include <dirent.h>
#    include <unistd.h>
#    define SEPARATOR '/'
#endifZipUtility::ZipResult ZipUtility::zipArchive(const std::vector<std::string>& filelist,const std::string& zipPath, CompressionMethod method,CompressionLevel level, const char* password,bool zip64, const char* globalComment)
{// 打开 ZIP 文件zipFile zf = zipOpen(zipPath.c_str(), APPEND_STATUS_CREATE);if (!zf) {return ZipResult::ERR_CREATE_ZIP;}int  err = ZIP_OK;char buf[8192];for (const auto& file : filelist) {// 打开本地文件FILE* fin = fopen(file.c_str(), "rb");if (!fin) {zipClose(zf, globalComment);return ZipResult::ERR_OPEN_FILE;}// 获取文件信息zip_fileinfo zi;memset(&zi, 0, sizeof(zi));time_t     current  = time(nullptr);struct tm* tm       = localtime(&current);zi.tmz_date.tm_sec  = tm->tm_sec;zi.tmz_date.tm_min  = tm->tm_min;zi.tmz_date.tm_hour = tm->tm_hour;zi.tmz_date.tm_mday = tm->tm_mday;zi.tmz_date.tm_mon  = tm->tm_mon;zi.tmz_date.tm_year = tm->tm_year + 1900;zi.dosDate          = static_cast<uLong>(current);// 提取文件名(不含路径)std::string filename = file;size_t      pos      = file.find_last_of("\\/");if (pos != std::string::npos) {filename = file.substr(pos + 1);}// 在ZIP中打开新文件int zip64flag = zip64 ? 1 : 0;err           = zipOpenNewFileInZip3_64(zf,filename.c_str(),&zi,nullptr,0,   // extrafield_localnullptr,0,         // extrafield_globalnullptr,   // commentstatic_cast<int>(method),static_cast<int>(level),0,               // raw-MAX_WBITS,      // windowBitsDEF_MEM_LEVEL,   // memLevelZ_DEFAULT_STRATEGY,password,0,   // crcForCryptingzip64flag);if (err != ZIP_OK) {fclose(fin);zipClose(zf, globalComment);return convertZipError(err);}// 读取并写入文件内容size_t read = 0;while ((read = fread(buf, 1, sizeof(buf), fin)) > 0) {err = zipWriteInFileInZip(zf, buf, static_cast<unsigned>(read));if (err != ZIP_OK) {fclose(fin);zipCloseFileInZip(zf);zipClose(zf, globalComment);return convertZipError(err);}}// 关闭ZIP中的文件err = zipCloseFileInZip(zf);if (err != ZIP_OK) {fclose(fin);zipClose(zf, globalComment);return convertZipError(err);}fclose(fin);}// 关闭ZIP文件err = zipClose(zf, globalComment);return convertZipError(err);
}ZipUtility::ZipResult ZipUtility::unzipArchive(const std::string& zipPath,const std::string& outDirectory,const char* password, bool restorePath)
{// 打开 ZIP 文件unzFile uf = unzOpen(zipPath.c_str());if (!uf) return ZipResult::ERR_OPEN_ZIP;// 创建输出目录createDirectory(outDirectory);// 获取ZIP文件信息unz_global_info64 gi;if (unzGetGlobalInfo64(uf, &gi) != UNZ_OK) {unzClose(uf);return ZipResult::ERR_INVALID_ZIP;}int  err = UNZ_OK;char filename_inzip[256];char buf[8192];// 遍历ZIP内所有文件for (uLong i = 0; i < gi.number_entry; i++) {unz_file_info64 file_info;err = unzGetCurrentFileInfo64(uf, &file_info, filename_inzip, sizeof(filename_inzip), nullptr, 0, nullptr, 0);if (err != UNZ_OK) {unzClose(uf);return convertUnzipError(err);}// 处理文件路径std::string fullPath = outDirectory;if (restorePath) {fullPath += SEPARATOR + std::string(filename_inzip);}else {// 仅提取文件名std::string fileName(filename_inzip);size_t      pos = fileName.find_last_of("\\/");if (pos != std::string::npos) {fileName = fileName.substr(pos + 1);}fullPath += SEPARATOR + fileName;}// 创建目录size_t pos = fullPath.find_last_of("\\/");if (pos != std::string::npos) {ZipResult dirResult = createDirectory(fullPath.substr(0, pos));if (dirResult != ZipResult::OK) {unzClose(uf);return dirResult;}}// 打开ZIP中的文件if (password) {err = unzOpenCurrentFilePassword(uf, password);}else {err = unzOpenCurrentFile(uf);}if (err != UNZ_OK) {unzClose(uf);return convertUnzipError(err);}// 创建输出文件FILE* fout = fopen(fullPath.c_str(), "wb");if (!fout) {unzCloseCurrentFile(uf);unzClose(uf);return ZipResult::ERR_WRITE_FILE;}// 解压文件内容do {err = unzReadCurrentFile(uf, buf, sizeof(buf));if (err < 0) break;if (err > 0) {if (fwrite(buf, 1, err, fout) != static_cast<size_t>(err)) {err = UNZ_ERRNO;break;}}} while (err > 0);fclose(fout);int closeErr = unzCloseCurrentFile(uf);// 处理CRC错误if (err == UNZ_CRCERROR || closeErr == UNZ_CRCERROR) {remove(fullPath.c_str());unzClose(uf);return ZipResult::ERR_CRC;}if (err != UNZ_OK && err != UNZ_EOF) {unzClose(uf);return convertUnzipError(err);}// 移动到下一个文件if (i < gi.number_entry - 1) {err = unzGoToNextFile(uf);if (err != UNZ_OK) {unzClose(uf);return convertUnzipError(err);}}}unzClose(uf);return ZipResult::OK;
}ZipUtility::ZipResult ZipUtility::createDirectory(const std::string& path)
{if (path.empty()) return ZipResult::OK;#ifdef _WIN32if (CreateDirectoryA(path.c_str(), NULL) || GetLastError() == ERROR_ALREADY_EXISTS) {return ZipResult::OK;}
#elsestruct stat st;if (stat(path.c_str(), &st) == 0) {if (S_ISDIR(st.st_mode)) return ZipResult::OK;return ZipResult::ERR_CREATE_DIR;}size_t pos = 0;do {pos                = path.find_first_of("\\/", pos + 1);std::string subdir = path.substr(0, pos);if (mkdir(subdir.c_str(), 0755) != 0 && errno != EEXIST) {return ZipResult::ERR_CREATE_DIR;}} while (pos != std::string::npos);
#endifreturn ZipResult::OK;
}ZipUtility::ZipResult ZipUtility::convertZipError(int err)
{switch (err) {case ZIP_OK: return ZipResult::OK;case ZIP_ERRNO: return ZipResult::ERR_OPEN_FILE;case ZIP_PARAMERROR: return ZipResult::ERR_INVALID_ZIP;case ZIP_BADZIPFILE: return ZipResult::ERR_INVALID_ZIP;case ZIP_INTERNALERROR: return ZipResult::ERR_WRITE_FILE;default: return ZipResult::ERR_UNKNOWN;}
}ZipUtility::ZipResult ZipUtility::convertUnzipError(int err)
{switch (err) {case UNZ_OK: return ZipResult::OK;case UNZ_ERRNO: return ZipResult::ERR_READ_FILE;case UNZ_PARAMERROR: return ZipResult::ERR_INVALID_ZIP;case UNZ_BADZIPFILE: return ZipResult::ERR_INVALID_ZIP;case UNZ_INTERNALERROR: return ZipResult::ERR_UNKNOWN;case UNZ_CRCERROR: return ZipResult::ERR_CRC;// case UNZ_BADPASSWORD: return ZipResult::ERR_INVALID_PASSWORD;default: return ZipResult::ERR_UNKNOWN;}
}
http://www.hskmm.com/?act=detail&tid=28656

相关文章:

  • 一看就懂,Oracle认证体系中的OCP中级认证
  • 2025 年试验机生产厂家最新推荐榜单:聚焦优质企业,助力精准选购高低温等各类试验设备弹簧拉压/弹簧疲劳/高频弹簧疲劳/U型弹簧专用试验机厂家推荐
  • IIS/如何查看IIS上部署网站的实时连接数
  • 拼叨叨砍价系统:实体店低成本引流的营销利器
  • 2025 自动门生产厂家最新推荐榜:权威筛选优质品牌,含选购指南与实力厂家深度解析
  • 医德出诊排班挂号管理系统:医院高效运营与便民服务的智能解决方案
  • 一佳教育培训课程系统小程序:一站式教育数字化解决方案
  • Supabase:无需后端代码的 Web 开发完整解决方案
  • Halo RAG!
  • SLS Copilot 实践:基于 SLS 灵活构建 LLM 应用的数据基础设施
  • 2025 木饰面源头厂家最新推荐榜单:21 年标杆企业领衔,背景墙/全屋 /格栅/碳晶板全品类最新推荐及选购指南
  • 2025 年北京市管道疏通公司最新推荐排行榜:覆盖多城区、高压技术赋能的优质企业优选榜单西城区/朝阳区/丰台区/石景山/海淀区管道疏通公司推荐
  • 2025 年北京市清理化粪池公司最新推荐排行榜:聚焦高压技术与全城服务的权威甄选朝阳区/丰台区/海淀区/通州区清理化粪池厂家推荐
  • 报表方案Stimulsoft 2025.4 重磅发布!新增AI报表助手、C#脚本支持、全新图表类型等多项功能!
  • Prometheus的Exporter的数据采集机制
  • 2025 年珠三角 / 中山 / 东莞 / 佛山厂房出售公司推荐:中创集团产业生态型厂房的价值与服务解析
  • CTFshow-web方向(更新中)
  • 拷贝和上传文件,涉及隐私协议
  • 2025储罐厂家,钢衬塑储罐,钢塑复合储罐,化工储罐,防腐储罐,PE储罐,盐酸储罐,硫酸储罐,聚丙烯储罐,不锈钢储罐,次氯酸钠储罐各类型最新推荐榜:品质卓越与技术创新的行业先锋!
  • 2025 年国内标志牌生产厂家最新推荐排行榜:聚焦优质企业助力客户精准选择道路/限速/公路/施工/警示/限高/三角/安全标志牌厂家推荐
  • 在Scala中,如何在泛型类中使用类型参数?
  • Maple 2025 来了!AI 赋能 + 6000 + 命令,破解数学计算、科研与教学痛点
  • 2025 护眼灯生产厂家最新推荐榜:精选五强资深与新锐品牌,深度解析品质口碑与选购指南
  • 2025 年护眼吸顶灯最新推荐榜:权威筛选五强品牌,技术与口碑双维度深度剖析
  • 2025 护眼台灯厂家最新推荐榜单:权威解析明可达等五强品牌,护眼参数与选购指南全攻略
  • 2025 年无线耳机源头厂家最新推荐榜单:覆盖头戴式 / 电竞 / 平价 / 电脑 / 游戏多品类且聚焦全产业链与精益制造的权威名录
  • 2025 年最新蓝牙耳机源头厂家口碑推荐榜:含琉璃 X 热销 64 万台企业及各类型高性价比品牌优选运动/真无线/头戴式/骨传导/游戏蓝牙耳机厂家推荐
  • 接口测试全流程实战:从工具到架构的深度解析
  • Node.js 负载均衡:构建高可用服务
  • C# Send and receive big file via stream