c++实现压缩解压 zip文件

article/2025/10/21 12:24:54

前言

由于博主需要写软件工程的课设,在没有好点子以及考虑到队友能力不足的情况下,决定写一个zip压缩解压。
我使用了Zip Utils实现这一想法,而我的代码则是对Zip Utils的封装,具体压缩和解压由Zip Utils实现。
画线的文件为Zip Utils提供
在这里插入图片描述

//zip_un.h
#pragma once
#include <Windows.h>          //添加Windows.h不然会一堆错误
#include <string>             //C++使用string添加string,不要添加string.h
#include <iostream>
#include <tchar.h>
#include <vector>
#include <io.h>
#include "zip.h"
#include "unzip.h"
using namespace std;class zip_un {
public:int Zip_UnPackFiles(string strZipPath);void Zip_PackFiles(string path);zip_un();private:HZIP hz;void browseFile(string inPath);WCHAR* toWchar(string strZipPath);vector<string> split(const string& str);string connectStr(vector<string>& ret);string temporaryPath;int index; //用于深度搜索的标识
};
//zip_un.cpp#include "zip_un.h"
zip_un::zip_un() {index = 0;
}
vector<string> zip_un::split(const string& str){vector<string> ret;const string pattern = "\\";/*if (pattern.empty())return ret;*/int stat = 0, index = str.find_first_of(pattern, 0);while (index != str.npos) {if (stat != index)ret.push_back(str.substr(stat, index - stat));stat = index + 1;index = str.find_first_of(pattern, stat);}if (!str.substr(stat).empty())ret.push_back(str.substr(stat));return ret;
}
string zip_un::connectStr(vector<string>& ret) {string c;ret[ret.size() - 1] = ret[ret.size() - 1].substr(0, ret[ret.size() - 1].size() - 4);for (int i = 0;i < ret.size();i++) {c += ret[i] + "\\";}return c;
}
int zip_un::Zip_UnPackFiles(string strZipPath){string strZipPath_un = strZipPath;vector<string> mUn = split(strZipPath);const WCHAR* pwUnicode = toWchar(strZipPath_un);string a = "mkdir -p " + connectStr(mUn);system(a.c_str());  //创建文件//解压文件//SetCurrentDirectory(_T("D:\\c++_project"));//将进程的工作目录移动到该参数所指的目录下,该目录为winrar.exe的默认文件路径SetCurrentDirectoryA(connectStr(mUn).c_str());//解压文件会直接在项目的.vcproj目录下进行HZIP hz = OpenZip(pwUnicode, NULL);ZIPENTRY ze;GetZipItem(hz, -1, &ze);  // -1给出关于zip文件整体信息int numitems = ze.index;for (int zi = 0; zi < numitems; zi++){ZIPENTRY ze;GetZipItem(hz, zi, &ze);UnzipItem(hz, zi, ze.name);cout << "解压成功" << endl;}CloseZip(hz);return 0;
}WCHAR* zip_un::toWchar(string strZipPath){string strZipPath_un = strZipPath;//将路径转为TCHAR类型int iUnicode = MultiByteToWideChar(CP_ACP, 0, strZipPath_un.c_str(), strZipPath_un.length(), NULL, 0);WCHAR* pwUnicode = new WCHAR[iUnicode + 2];if (pwUnicode){ZeroMemory(pwUnicode, iUnicode + 2);}MultiByteToWideChar(CP_ACP, 0, strZipPath_un.c_str(), strZipPath_un.length(), pwUnicode, iUnicode);pwUnicode[iUnicode] = '\0';pwUnicode[iUnicode + 1] = '\0';return pwUnicode;
}void zip_un::browseFile(string inPath) {string path = inPath + "\\*.*", filePath;struct _finddata_t fileinfo;long handle = _findfirst(path.c_str(), &fileinfo);if (handle == -1)exit(0);do{if (fileinfo.attrib & _A_SUBDIR){  //为目录//ZipAddFolder(hz, toWchar(fileinfo.name));		if (strcmp(fileinfo.name, ".") == 0 || strcmp(fileinfo.name, "..") == 0) continue;index++;vector<string> aa = split(inPath);for (int i = index;i > 0;i--) {temporaryPath = temporaryPath + aa[aa.size() - i] + "\\";}ZipAddFolder(hz, toWchar(temporaryPath + fileinfo.name));temporaryPath.clear();string dirNew = inPath + "\\" + fileinfo.name;browseFile(dirNew);	index--;} else {filePath = inPath + "\\" ;vector<string> aa = split(inPath);for (int i = index+1;i > 0;i--) {temporaryPath = temporaryPath + aa[aa.size() - i] + "\\";}ZipAdd(hz, toWchar(temporaryPath + fileinfo.name  ), toWchar(filePath  + fileinfo.name));temporaryPath.clear();}} while (!_findnext(handle, &fileinfo));_findclose(handle);
}void zip_un::Zip_PackFiles(string path) {string  path_1 = path + ".zip";hz = CreateZip(toWchar(path_1), 0);browseFile(path);CloseZip(hz);
}

包含了压缩解压的实现,下面是样例

#include"zip_un.h"
#include <CString>
void main() {zip_un z;string strZipPath_un, strZipPath = "D:\\c++_project\\2";strZipPath_un = strZipPath + ".zip";z.Zip_PackFiles(strZipPath);
}

在这里插入图片描述
在这里插入图片描述
受限于个人能力不足和Zip Utils的局限,无法解决中文压缩后乱码的问题。

需要注意的是压缩路径的问题,我没有想出特别好的办法。我使用深度搜索,解决这一问题。

参考两位博主的博客
https://blog.csdn.net/qq_37059136/article/details/83510764
使用博主 string转换为 WCHAR* 的思路
https://blog.csdn.net/struggling_jeff/article/details/100857364
参考博主的代码结构
https://www.codeproject.com/Articles/7530/Zip-Utils-Clean-Elegant-Simple-Cplusplus-Win
官方链接


http://chatgpt.dhexx.cn/article/JFpGuK8U.shtml

相关文章

aistudio解压zip

最近整软件杯&#xff0c;在aistudio上传数据跑代码&#xff0c;还在上传数据阶段&#xff0c;好像太大的包只能一个一个上传&#xff0c;图片啥的只能上传五个&#xff0c;就很离谱呀 方法一&#xff1a; 这样可以直接解压到本地目录 方法二&#xff1a; 百度看了一下可以…

【ubuntu】ubuntu 如何解压zip文件

目录 1、安装 unzip 2、解压 1、安装 unzip sudo apt-get install unzip 2、解压 unzip 文件命.zip

cmd解压zip文件

自动化脚本执行过程中需要分析log,遇到压缩文件时需要先将文件解压&#xff0c;Ubuntu系统自带解压缩&#xff0c;但windows需要自己下载配置&#xff0c;以下是Windows系统中安装使用解压缩的方法&#xff1a; 一.安装 1、下载unzip安装包&#xff1a;http://gnuwin32.sourcef…

java解压zip文件

zip文件目录结构如下&#xff1a; 其中&#xff0c;word下面还有多个子目录&#xff0c;可以递归进行解压目录&#xff0c;核心代码如下&#xff1a; package com.atguigu.common.utils;import java.io.File; import java.io.FileOutputStream; import java.io.IOException; i…

Linux 解压 zip 分卷

对于一个大的文件&#xff0c;使用分卷压缩得到如下文件&#xff1a; 传到Linux目录下&#xff0c;希望解压出来&#xff0c;需要使用zip -F命令修复分卷&#xff0c;从而合成正确的一个压缩文件 zip -F UCF-101.zip --out ucf101.zip得到 ucf101.zip&#xff0c;然后解压 uc…

java解压zip压缩包

坐在旁边的小伙伴问我怎么用 java 将服务器上的压缩文件解压出来&#xff0c;我索性给他写了个 demo &#xff0c;也顺手记录一下。亲测可用&#xff0c;如果觉得有帮助&#xff0c;欢迎点赞&#xff0c;评论&#xff0c;收藏&#xff0c;转发 java 实现解压 zip 压缩包 packag…

【方法】ZIP分卷压缩文件如何解压?

有时候文件太大&#xff0c;我们在压缩ZIP文件时会使用分卷压缩。 那后期想要解压ZIP分卷文件&#xff0c;要如何解压呢&#xff1f;不清楚的小伙伴&#xff0c;可以跟着小编来看看哦。 在解压ZIP分卷压缩文件之前&#xff0c;要先确保所有的分卷压缩包都存放在一个文件夹里&…

Btrace安装步骤[详细]

提示&#xff1a;文章写完后&#xff0c;目录可以自动生成&#xff0c;如何生成可参考右边的帮助文档 Btrace安装步骤 一、下载地址二、配置环境变量1.添加home2.加到Path里3.尝试启动4.Windows查看pid进程号 一、下载地址 我用的是最新版本 V2.2.1 下载地址: https://github.…

【性能跟踪】btrace学习一--安装btrace

最近公司里为了监控线上的类&#xff0c;方法的执行时间&#xff0c;结果等用到了这个开源的小工具&#xff0c;自己也学习了下&#xff0c;感觉比较简单&#xff0c;但是还是挺实用的。 这里做个学习笔记。 1.首先到网上下个Btrace包吧&#xff0c;官方网址是&#xff1a;htt…

重要升级!btrace 2.0 技术原理大揭秘

‍ 动手点关注 干货不迷路 项目 GitHub 地址&#xff1a;https://github.com/bytedance/btrace 背景介绍 在一年多前&#xff0c;我们对外正式开源了 btrace&#xff08;AKA RheaTrace&#xff09;&#xff0c;它是基于 Systrace 的高性能 Trace 工具&#xff0c;目前字节跳动已…

BTrace安装和使用

一、 安装JDK&#xff08;需要安装jdk7及以上版本&#xff09; jdk-7u80-linux-x64.tar.gz 二、 安装BTrace 1&#xff09;下载地址&#xff1a;https://github.com/btraceio/btrace/releases/tag/v1.3.8.3-1 2&#xff09;解压缩 在当前用户目录下新建btrace目录&#…

BTrace分析和使用

BTrace分析和使用 一、 BTrace简介 BTrace是一个为Java平台开发的安全、动态的追踪工具。BTrace动态地向目标应用程序的字节码注入Java追踪代码&#xff08;字节码追踪&#xff09;。 GitHUB地址https://github.com/btraceio/btrace。 原理为将字节码发送到应用&#xff…

BTrace入门

BTrace入门 概念性的东西自行网上查阅吧。 安装 现在btrace项目已经迁移到了github上了。这点可以从官网&#xff08; https://kenai.com/projects/btrace/downloads/directory/releases/latest &#xff09;上看出。 点开这个链接就会跳转到btrace项目的github地址&#x…

(转)btrace使用

Btrace 是一个安全&#xff0c;可以动态跟踪 java 程序的一种工具。他的操作不会对原有 java 进程产生影响&#xff0c;不用关闭正在运行的 java 进程&#xff0c;也不会修改 java 进程中的逻辑和数据。因此&#xff0c;也就成为我们线上跟踪生产代码的有力工具! 之前 Btrace 只…

【性能跟踪】btrace学习二--btrace一个简单例子

btrace安装好了&#xff0c;就看一些语法吧。可以到btrace的官方帮助文档中去看&#xff0c;基本上就可以http://kenai.com/projects/btrace/pages/UserGuide 【注&#xff1a;你本地的jdk要是1.6及以上才行&#xff0c;1.5可是不支持的】 下面以一个例子来说明&#xff1a; 新…

btrace 开源!基于 Systrace 高性能 Trace 工具

介绍 btrace&#xff08;又名 RheaTrace&#xff09; 是抖音基础技术团队自研的一款高性能 Android Trace 工具&#xff0c;它基于 Systrace 实现&#xff0c;并针对 Systrace 不足之处加以改进&#xff0c;核心改进点如下。 效率提升&#xff1a;编译期间为 App 方法自动注入自…

BTrace简介与使用说明

目录 前言 简介 安装 下载BTrace 配置BTRACE_HOME BTrace使用 注意事项 BTrace注解 ProbeClassName ProbeMethodName Self Return Duration TargetInstance TargetMethodOrField OnMethod OnTimer OnError OnEvent 在jvisualvm中使用BTrace BTraceUtils方法介绍 前言 大家在…

Btrace使用入门

目录 1.什么是BTrace 2.BTrace使用场景 BTrace可以做什么&#xff1f; 3.使用限制 4.使用方法及样例 4.1使用方法 4.2使用样例 2.查看哪些方法调用了 System.gc() &#xff0c;调用栈是怎样的 3.打印某个类中某一方法的入参和返回值 5.拦截方法定义 定位方法 拦截时…

黄油刀ButterKnife的使用

1、ButterKnife是一个专注于Android系统的View注入框架,以前总是要写很多findViewById来找到View对象&#xff0c;有了ButterKnife可以很轻松的省去这些步骤。是大神JakeWharton的力作&#xff0c;目前使用很广。最重要的一点&#xff0c;使用ButterKnife对性能基本没有损失&am…

Butterknife——黄油刀的基本使用

平常我们在做项目的时候&#xff0c;不可避免地一定会用到findviewbyid()方法&#xff0c;写多了还是比较繁琐的。但是有个大神Jake Wharton开源了一个神奇的框架叫做ButterKnife,炒鸡好用 接下来是黄油刀的基本用法 &#xff1a; 首先配置编译环境&#xff1a; 1.在eclipse…