使用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);}
}
相关文章:
使用Zlib库进行多文件或者多文件夹的压缩解压缩
zlib库可在git上自己clone下来然后使用cmake工具生成解决方案,编译、生成zlib二进制文件。然后将zlib库引入项目: //zlib库支持 #include "../zlib/include/zlib.h" #ifdef _DEBUG #pragma comment(lib, "../zlib/lib/zlibd.lib") …...
CSGHub携手Nvidia NIM、阿里计算巢打造企业级私有化部署解决方案
强强联合 人工智能与大数据的迅速发展,大模型的推理应用和资产管理已成为企业数字化转型的重要组成部分,企业正寻求高效、安全的AI模型部署解决方案。为应对日益增长的计算需求和复杂的数据管理挑战,CSGHub、Nvidia和阿里云计算巢强强联手&a…...
opencv的球面投影
cv::detail::SphericalProjector 在全景图像拼接任务中,可能需要对多个图像进行球面投影以实现无缝拼接。每个cv::detail::SphericalProjector可以负责一个图像的球面投影操作。通过将多个这样的投影器存储在std::vector中,可以对一组图像依次进行投影处…...
5. 去中心化应用(dApp)
去中心化应用(dApp) 去中心化应用(dApp)是基于区块链技术构建的应用程序,其核心特性是去中心化、透明和开放。dApp与传统应用有许多显著的区别,它们在实现和功能上都带来了新的变革。以下是对dApp的详细介…...
k8s服务发布Ingress
Kubernetes暴露服务的方式目前只有三种:LoadBlancer Service、NodePort Service、Ingress,通俗来讲,ingress和之前提到的Service、Deployment,也是一个k8s的资源类型,ingress用于实现用域名的方式访问k8s内部应用。 In…...
区块链学习笔记1--比特币
区块链是分布式数据存储、点对点传输、共识机制、加密算法等计算机技术的新型应用模式。 从狭义上来说:区块链是一种按照时间顺序将数据区块以顺序相连的方式组合成的一种链式数据结构,并以密码学的方式保证的不可篡改和不可伪造的分布式账本。 意思就是…...
在 Vite 项目中自动为每个 Vue 文件导入 base.less
在 Vue.js 项目中,使用 Less 作为 CSS 预处理器时,我们通常会创建一个全局的样式文件(如 base.less),用于存放一些全局变量、混合、通用样式等。为了避免在每个 Vue 组件中手动导入这个文件,我们可以通过配…...
RUST 学习之全局变量
RUST 全局变量 rust 全局变量编译期初始化的全局变量静态常量静态变量原子类型的静态变量 运行期初始化的全局变量lazy_staticBox::leakOnceCell & OnceLock 参考文档 rust 全局变量 编译期初始化的全局变量 静态常量 在编译期初始化,所以其赋值只能是表达式…...
代码随想录八股训练营第三十九天| C++
前言 一、说一下 lambda函数? 1.1.Lambda 函数的一般语法如下: 1.2.捕获子句: 二、C 怎么实现一个单例模式? 2.1.懒汉式(线程不安全): 2.2.饿汉式(线程安全): 2.3.双重检查锁定ÿ…...
服务网关工作原理,如何获取用户真实IP?
文章目录 一、什么是网关二、网关工作原理 (★)三、SpringCloud Gateway3.1 Gateway 简介3.2 Gateway 环境搭建3.3 自定义路由规则 (★)3.4 局部过滤器3.5 全局过滤器(案例:获取用户真实IP地址) (★) 补充1:不同类型的客户端如何设…...
单链表的实现(C语言)
目录 1.单链表 1.1 实现单链表 1.1.1 文件创建 1.1.2 链表功能了解 1.1.3 链表的结点 1.1.4 链表的函数声明 1.1.5 链表功能的实现 链表是一种链式结构,物理结构不连续,逻辑结构是连续的,在计算机中链表的实际存储是按照一个结点内存放…...
sql语句的训练2024/9/9
1题 需要看清思路:不是将数据库中的device_id的名字改为user_infors_example,而是在查找的时候,需要将device_id看成user_infors_example来进行查找。 答案 select device_id AS user_infos_example FROM user_profile limit 2 2 当固定查找…...
【QT】常用控件-下
欢迎来到Cefler的博客😁 🕌博客主页:折纸花满衣 🏠个人专栏:QT 目录 👉🏻QComboBox👉🏻 QSpinBox👉🏻QDateTimeEdit👉🏻QD…...
828华为云征文|华为云Flexus X实例docker部署Jitsi构建属于自己的音视频会议系统
828华为云征文|华为云Flexus X实例docker部署Jitsi构建属于自己的音视频会议系统 华为云最近正在举办828 B2B企业节,Flexus X实例的促销力度非常大,特别适合那些对算力性能有高要求的小伙伴。如果你有自建MySQL、Redis、Nginx等服务的需求&a…...
25虾皮笔试shopee笔试测评sea笔试测评题型
虾皮笔试shopee笔试测评用的自己的笔试系统,全英文笔试: 1.Numerical Reasoning Test:10题,言语推断和数学计算 2. Verbal Reasoning Test:10题,言语理解,每题一段英文材料,选对错…...
启明云端乐鑫代理商,乐鑫ESP32无线芯片方案,物联网设备WiFi联动控制
随着智能和远程技术的飞速发展,物联网(IoT)逐渐出现在我们生活的每一个角落。乐鑫以其创新的无线通信技术,正成为智能家居、工业自动化和医疗设备等领域的推动者。 无线WiFi芯片模组不仅提供了强大的数据处理能力,还赋予了设备以直观的交互方…...
希尔排序/选择排序
前言: 本篇主要对常见的排序算法进行简要分析,代码中均以数组 arr[] { 5, 3, 9, 6, 2, 4, 7, 1, 8 } 为例,进行升序排列。 常见的排序算法有如下: 选择排序中,直接选择排序没有任何实际与教育意义,而堆排…...
漫谈设计模式 [16]:中介者模式
引导性开场 菜鸟:老鸟,我最近在开发一个聊天应用的时候遇到了点问题。每个用户都需要与其他用户直接通信,这让我在代码中写了很多复杂的逻辑来管理这些联系。这样下去,代码越来越难维护了。你有什么建议吗? 老鸟&…...
深度学习-物体检测YOLO(You only look once)
目录 一:YOLO算法的网络结构 流程 1.图像分割 2.图片在网格中的处理 3.非极大值抑制 二:训练 三:分类误差 四:与Faster R-CNN对比 一:YOLO算法的网络结构 GooleNet4个卷积2个全连接层 流程 输入原始图片resize到…...
redisson中的分布式锁
我的博客大纲 我的后端学习大纲 a.redisson概述: 1.Redisson是一个在Redis的基础上实现的Java驻内存数据网格(In-Memory Data Grid)2.redisson介绍官方文档地址:3.Redisson它不仅提供了一系列的分布式的Java常用对象,还…...
多云管理“拦路虎”:深入解析网络互联、身份同步与成本可视化的技术复杂度
一、引言:多云环境的技术复杂性本质 企业采用多云策略已从技术选型升维至生存刚需。当业务系统分散部署在多个云平台时,基础设施的技术债呈现指数级积累。网络连接、身份认证、成本管理这三大核心挑战相互嵌套:跨云网络构建数据…...
7.4.分块查找
一.分块查找的算法思想: 1.实例: 以上述图片的顺序表为例, 该顺序表的数据元素从整体来看是乱序的,但如果把这些数据元素分成一块一块的小区间, 第一个区间[0,1]索引上的数据元素都是小于等于10的, 第二…...
Flask RESTful 示例
目录 1. 环境准备2. 安装依赖3. 修改main.py4. 运行应用5. API使用示例获取所有任务获取单个任务创建新任务更新任务删除任务 中文乱码问题: 下面创建一个简单的Flask RESTful API示例。首先,我们需要创建环境,安装必要的依赖,然后…...
【OSG学习笔记】Day 18: 碰撞检测与物理交互
物理引擎(Physics Engine) 物理引擎 是一种通过计算机模拟物理规律(如力学、碰撞、重力、流体动力学等)的软件工具或库。 它的核心目标是在虚拟环境中逼真地模拟物体的运动和交互,广泛应用于 游戏开发、动画制作、虚…...
黑马Mybatis
Mybatis 表现层:页面展示 业务层:逻辑处理 持久层:持久数据化保存 在这里插入图片描述 Mybatis快速入门  C 中的命名空间 与 C 语言的对比 二、缺省参数 C 中的缺省参数 与 C 语言的对比 三、引用(reference) C 中的引用 与 C 语言的对比 四、inline(内联函数…...
鱼香ros docker配置镜像报错:https://registry-1.docker.io/v2/
使用鱼香ros一件安装docker时的https://registry-1.docker.io/v2/问题 一键安装指令 wget http://fishros.com/install -O fishros && . fishros出现问题:docker pull 失败 网络不同,需要使用镜像源 按照如下步骤操作 sudo vi /etc/docker/dae…...
用docker来安装部署freeswitch记录
今天刚才测试一个callcenter的项目,所以尝试安装freeswitch 1、使用轩辕镜像 - 中国开发者首选的专业 Docker 镜像加速服务平台 编辑下面/etc/docker/daemon.json文件为 {"registry-mirrors": ["https://docker.xuanyuan.me"] }同时可以进入轩…...
.Net Framework 4/C# 关键字(非常用,持续更新...)
一、is 关键字 is 关键字用于检查对象是否于给定类型兼容,如果兼容将返回 true,如果不兼容则返回 false,在进行类型转换前,可以先使用 is 关键字判断对象是否与指定类型兼容,如果兼容才进行转换,这样的转换是安全的。 例如有:首先创建一个字符串对象,然后将字符串对象隐…...
