前言

由于博主需要写软件工程的课设,在没有好点子以及考虑到队友能力不足的情况下,决定写一个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文件相关推荐

  1. android zip格式应用,Android 压缩解压zip文件

    Android 压缩解压zip文件 上次写了个解压缩功能,但有局限性,比如压缩文件xx.zip 里包括子目录的情况下,执行上次解压缩的功能就不能实现我们想要的效果,于是在网上参考了一下java的解压缩 ...

  2. 使用InfoZip压缩解压zip文件

    使用InfoZip压缩解压zip文件 源码下载地址:http://download.csdn.net/detail/risingsun001/4254821 先贴部分源码: void CInfozip ...

  3. VC++压缩解压zip文件(支持密码)

    只能压缩解压zip格式的,不需要dll或者库文件,核心是HZIP,支持带密码压缩解压(但是有时不完美,属于HZIP本身的问题,请不要纠结这一点). 以下为核心封装文件,建议大家下载源代码查看,下载地址 ...

  4. C#压缩解压zip 文件

    我在做项目的时候需要将文件进行压缩和解压缩,于是就从http://www.icsharpcode.net下载了关于压缩和解压缩的源码,但是下载下来后,面对这么多的代码,一时不知如何下手.只好耐下心来, ...

  5. 压缩解压zip文件包

    import java.io.*;import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory ...

  6. liunx 下压缩解压zip文件

    压缩 1.把/home目录下面的mydata目录压缩为mydata.zip zip -r mydata.zip mydata #压缩mydata目录 2.把/home目录下面的mydata.zip解压 ...

  7. Scala压缩解压Zip文件

    package com.sm.utilsimport java.io.{File, FileInputStream, FileOutputStream, BufferedInputStream} im ...

  8. Android 解压zip文件

    过了n多天后,当再次使用原先博客上写的那篇: Android 压缩解压zip文件 去做zip包的解压的时候,出现了原来没有发现的很多问题.首先是中文汉字问题,使用java的zip包不能很好的解决解压问 ...

  9. java csv文件tozip后损坏_java上传并下载以及解压zip文件有时会报文件被损坏错误分析以及解决...

    情景描述: 1.将本地数据备份成zip文件: 2.将备份的zip文件通过sftp上传到文件服务器: 3.将文件服务器上的zip文件下载到运行服务器: 4.将下载的zip文件解压到本地(文件大小超过50 ...

  10. java解压zip文件,处理文件名不能为中文

    1.最近工作需要把压缩文件解压,经过测试有两种方法,一种是JDK自带的ZipFile,       另外一种是org.apache.tools.zip进行解压. 2.经测试,JDK自带的文件不能处理文 ...

最新文章

  1. 数据企业IBM段仰圣:大数据关键是“分析”
  2. A*寻路算法的探寻与改良(三)
  3. 老板评价下属的普遍性原则
  4. “新SaaS”引爆产业奇点《2017中国SaaS用户研究报告》
  5. LINUX下的文件结构介绍
  6. 一切为了孩子——一位IT麻麻的新西兰移民记录
  7. Java transient关键字(序列化避免被反序列化获取敏感信息)
  8. LibLinear(SVM包)使用说明之(二)MATLAB接口
  9. C++_类和对象_对象特性_This指针的用途_用来解决名称冲突_*this实现链式编程---C++语言工作笔记049
  10. php必填参数校验,laravel请求参数校验方法
  11. Python中字母大小写转换
  12. linux常用命令(2)关机重启 文本编辑器 系统管理 软件安装
  13. 【pytest】之parameterize()参数化,实现测试方法数据化
  14. macbook使用automator实现文文本自动化处理
  15. 【火炉炼AI】机器学习008-简单线性分类器解决二分类问题
  16. 示波器FFT频谱分析的使用方法和注意点
  17. SAP ABAP SD常用函数或BAPI
  18. 软件工程师能走多远?
  19. “两岸四地消费者信心指数”:消费者信心波动
  20. 谷歌浏览器,上传文件卡死(无响应)

热门文章

  1. 三菱plc串口通讯c语言,三菱plc串口通信协议与串口初始化
  2. 地产行业主数据建设项目思考
  3. ipython安装成功后用不了_Python常用工具ipython安装与使用
  4. ENVI/IDL 批量裁剪同一地区的多幅影像-第五篇
  5. 工程分析:Kconfig
  6. 点击图片实现图片放大
  7. MyBatis的插件
  8. Lua EmmyLua 注解详解
  9. 软件体系结构复习指南
  10. 【SW】利用3D打印机打印 PCB 钢网的方法