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

使用Zlib库进行多文件或者多文件夹的压缩解压缩

zlib库可在git上自己clone下来然后使用cmake工具生成解决方案,编译、生成zlib二进制文件。然后将zlib库引入项目:

//zlib库支持
#include "../zlib/include/zlib.h"
#ifdef _DEBUG
#pragma comment(lib, "../zlib/lib/zlibd.lib")
#else
#pragma comment(lib, "../zlib/lib/zlib.lib")
#endif

定义一个文件结构

typedef struct tagZipperFileInfo
{char m_szLocalPath[MAX_PATH];char m_szRootPath[MAX_PATH];char m_szFileName[MAX_PATH];size_t m_FileSize;
}ZipperFileInfo;

压缩文件相关:

/*
*	strFolder	需要被压缩的文件夹
*	strOut		保存的文件
*/
void CompressFolder(std::string& strFolder, std::string& strOut)
{//创建压缩的目标文件std::ofstream dest(strOut, std::ios::binary | std::ios::trunc);if (!dest.is_open()) {//errorreturn;}dest.close();std::vector<ZipperFileInfo> vecZipperFiles;//遍历文件夹下的所有文件OperateFolder(strFolder, strFolder, vecZipperFiles);//压缩文件gzFile gzOut = gzopen(strOut.c_str(), "wb");CompressFiles(vecZipperFiles, gzOut);gzclose(gzOut);
}
void OperateFolder(std::string& strFolder, std::string& strRoot, std::vector<ZipperFileInfo>& vecZipperFiles)
{std::string searchPath = strFolder + "\\*";WIN32_FIND_DATAA findData;HANDLE hFind = FindFirstFileA(searchPath.c_str(), &findData);if (hFind == INVALID_HANDLE_VALUE) {//errorreturn;}do {if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {if (strcmp(findData.cFileName, ".") != 0 && strcmp(findData.cFileName, "..") != 0) {std::string subFolderPath = strFolder + "\\" + findData.cFileName;OperateFolder(subFolderPath, strRoot, vecZipperFiles);}}else {std::string strLocalPath = strFolder + "\\" + findData.cFileName;std::string strFileRootPath = strFolder;std::string strFileName = findData.cFileName;//需要根据strRoot分割出来需要压缩文件的相对路径名std::string strTempPath;size_t start = strFileRootPath.find(strRoot);if (start == std::string::npos)strTempPath = strFileRootPath; // 如果找不到根路径,则返回完整路径else{if (strFileRootPath == strRoot)strTempPath = strFileRootPath.substr(start + strRoot.length());elsestrTempPath = strFileRootPath.substr(start + strRoot.length() + 1);}ZipperFileInfo zipperFile;memcpy(zipperFile.m_szRootPath, strTempPath.c_str(), MAX_PATH);memcpy(zipperFile.m_szLocalPath, strLocalPath.c_str(), MAX_PATH);memcpy(zipperFile.m_szFileName, strFileName.c_str(), MAX_PATH);//计算文件大小std::ifstream in(strLocalPath, std::ios::binary | std::ios::ate);size_t fileSize = in.tellg();in.seekg(0);in.close();zipperFile.m_FileSize = fileSize;vecZipperFiles.push_back(zipperFile);}} while (FindNextFileA(hFind, &findData) != 0);FindClose(hFind);
}

压缩文件:

void CompressFiles(std::vector<ZipperFileInfo>& vecZipperFiles, gzFile& gzOut)
{int nFileCount = vecZipperFiles.size();gzwrite(gzOut, reinterpret_cast<const void*>(&nFileCount), 4);gzwrite(gzOut, reinterpret_cast<const void*>(vecZipperFiles.data()), vecZipperFiles.size() * sizeof(ZipperFileInfo));//再往压缩文件中写入需要压缩为文件内容for (int i = 0; i < vecZipperFiles.size(); i++){std::string strLocalPath = vecZipperFiles[i].m_szLocalPath;std::ifstream infile(strLocalPath, std::ios::binary);char buffer[4096];while (infile){infile.read(buffer, sizeof(buffer));auto bytes = infile.gcount();if (bytes > 0){//写入目标压缩文件gzwrite(gzOut, buffer, bytes);}}infile.close();}
}

文件解压相关:

//strFilePath 压缩文件路径
void DecompressFiles(std::string& strFilePath)
{gzFile gzin = gzopen(strFilePath.c_str(), "rb");if (!gzin) return; //open errorint nFileCount = 0;gzread(gzin, &nFileCount, 4); //读取压缩的文件数量// 读取文件列表信息std::vector<ZipperFileInfo> vecZipperFiles;ZipperFileInfo zipperFile;for (int i = 0; i < nFileCount; i++){if (gzread(gzin, &zipperFile, sizeof(ZipperFileInfo)) == sizeof(ZipperFileInfo))vecZipperFiles.push_back(zipperFile);}SStringW sstrAppPath = CGlobalUnits::GetInstance()->m_sstrAppPath;std::string strAppPath = S_CW2A(sstrAppPath);//先创建个输出目录size_t szPos = strFilePath.find_last_of("\\");if (szPos != std::string::npos){std::string strTmp = strFilePath.substr(szPos + 1);//分解出namesize_t szName = strTmp.find_last_of(".");if (szName != std::string::npos){std::string strName = strTmp.substr(0, szName);strAppPath += strName;}}CreateDirectoryA(strAppPath.c_str(), NULL);//解压文件for (int i = 0; i < vecZipperFiles.size(); i++){ZipperFileInfo& info = vecZipperFiles[i];std::string strRoot = info.m_szRootPath;std::string strPath;if (strRoot == "")  //根目录下strPath = strAppPath + "\\" + info.m_szFileName;else{CreateFolder(strAppPath, strRoot);strPath = strAppPath + "\\" + strRoot + "\\" + info.m_szFileName;}std::ofstream outFile(strPath, std::ios::binary);if (!outFile) continue;  //error char buffer[4096];size_t fileSize = info.m_FileSize;while (fileSize > 0){size_t bytesToRead = std::min(static_cast<size_t>(4096), fileSize);int bytesRead = gzread(gzin, buffer, bytesToRead);if (bytesRead <= 0) break; //read erroroutFile.write(buffer, bytesRead);fileSize -= bytesRead;}outFile.close();}gzclose(gzin);
}

递归生成压缩文件中的目录结构:

/*
*	strRoot	解压缩的目标目录
*	strDir	压缩文件的相对路径
*/
void CreateFolder(std::string& strRoot, std::string& strDir)
{size_t szPos = strDir.find_first_of("\\");if (szPos != std::string::npos){std::string strName = strDir.substr(0, szPos);std::string strPath = strRoot + "\\" + strName;CreateDirectoryA(strPath.c_str(), NULL);std::string strSubName = strDir.substr(szPos + 1);std::string strTempRoot = strRoot + "\\" + strName;CreateFolder(strTempRoot, strSubName);}else{std::string strPath = strRoot + "\\" + strDir;CreateDirectoryA(strPath.c_str(), NULL);}
}
http://www.lryc.cn/news/435677.html

相关文章:

  • CSGHub携手Nvidia NIM、阿里计算巢打造企业级私有化部署解决方案
  • opencv的球面投影
  • 5. 去中心化应用(dApp)
  • k8s服务发布Ingress
  • 区块链学习笔记1--比特币
  • 在 Vite 项目中自动为每个 Vue 文件导入 base.less
  • RUST 学习之全局变量
  • 代码随想录八股训练营第三十九天| C++
  • 服务网关工作原理,如何获取用户真实IP?
  • 单链表的实现(C语言)
  • sql语句的训练2024/9/9
  • 【QT】常用控件-下
  • 828华为云征文|华为云Flexus X实例docker部署Jitsi构建属于自己的音视频会议系统
  • 25虾皮笔试shopee笔试测评sea笔试测评题型
  • 启明云端乐鑫代理商,乐鑫ESP32无线芯片方案,物联网设备WiFi联动控制
  • 希尔排序/选择排序
  • 漫谈设计模式 [16]:中介者模式
  • 深度学习-物体检测YOLO(You only look once)
  • redisson中的分布式锁
  • 如何将镜像推送到docker hub
  • GO 匿名函数
  • JuiceFS 在多云架构中加速大模型推理
  • 【DCL】Dual Contrastive Learning for General Face Forgery Detection
  • https的特点
  • 〖open-mmlab: MMDetection〗解析文件:mmdet/models/losses/cross_entropy_loss.py
  • 【PyTorch单点知识】torch.nn.Embedding模块介绍:理解词向量与实现
  • Jedis 操作 Redis 数据结构全攻略
  • ctf.show靶场ssrf攻略
  • 在 PyTorch 中,如何使用 `pack_padded_sequence` 来提高模型训练的效率?
  • Gossip协议