c++实现压缩解压 zip文件
前言
由于博主需要写软件工程的课设,在没有好点子以及考虑到队友能力不足的情况下,决定写一个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
官方链接
c++实现压缩解压 zip文件相关推荐
- android zip格式应用,Android 压缩解压zip文件
Android 压缩解压zip文件 上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩 ...
- 使用InfoZip压缩解压zip文件
使用InfoZip压缩解压zip文件 源码下载地址:http://download.csdn.net/detail/risingsun001/4254821 先贴部分源码: void CInfozip ...
- VC++压缩解压zip文件(支持密码)
只能压缩解压zip格式的,不需要dll或者库文件,核心是HZIP,支持带密码压缩解压(但是有时不完美,属于HZIP本身的问题,请不要纠结这一点). 以下为核心封装文件,建议大家下载源代码查看,下载地址 ...
- C#压缩解压zip 文件
我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...
- 压缩解压zip文件包
import java.io.*;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory ...
- liunx 下压缩解压zip文件
压缩 1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录 2.把/home目录下面的mydata.zip解压 ...
- Scala压缩解压Zip文件
package com.sm.utilsimport java.io.{File, FileInputStream, FileOutputStream, BufferedInputStream} im ...
- Android 解压zip文件
过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...
- java csv文件tozip后损坏_java上传并下载以及解压zip文件有时会报文件被损坏错误分析以及解决...
情景描述: 1.将本地数据备份成zip文件: 2.将备份的zip文件通过sftp上传到文件服务器: 3.将文件服务器上的zip文件下载到运行服务器: 4.将下载的zip文件解压到本地(文件大小超过50 ...
- java解压zip文件,处理文件名不能为中文
1.最近工作需要把压缩文件解压,经过测试有两种方法,一种是JDK自带的ZipFile, 另外一种是org.apache.tools.zip进行解压. 2.经测试,JDK自带的文件不能处理文 ...
最新文章
- 数据企业IBM段仰圣:大数据关键是“分析”
- A*寻路算法的探寻与改良(三)
- 老板评价下属的普遍性原则
- “新SaaS”引爆产业奇点《2017中国SaaS用户研究报告》
- LINUX下的文件结构介绍
- 一切为了孩子——一位IT麻麻的新西兰移民记录
- Java transient关键字(序列化避免被反序列化获取敏感信息)
- LibLinear(SVM包)使用说明之(二)MATLAB接口
- C++_类和对象_对象特性_This指针的用途_用来解决名称冲突_*this实现链式编程---C++语言工作笔记049
- php必填参数校验,laravel请求参数校验方法
- Python中字母大小写转换
- linux常用命令(2)关机重启 文本编辑器 系统管理 软件安装
- 【pytest】之parameterize()参数化,实现测试方法数据化
- macbook使用automator实现文文本自动化处理
- 【火炉炼AI】机器学习008-简单线性分类器解决二分类问题
- 示波器FFT频谱分析的使用方法和注意点
- SAP ABAP SD常用函数或BAPI
- 软件工程师能走多远?
- “两岸四地消费者信心指数”:消费者信心波动
- 谷歌浏览器,上传文件卡死(无响应)