c++ 实现压缩

简介

  • 目标:使用c++压缩文件夹
  • 方法:调用exe来实现的压缩。这里调用的是自己编译的minizip.exe。
  • 环境:win10/win7,visual studio 2019
  • 资源:https://github.com/ltCodeW/minizip
  • 注意:这个minizip是我自己编译的,有个问题,压缩文件路径中不能有中文字符,给各位看官挖个坑(阴笑)。简单的解决方法是使用7zip替换我的这个minizip。

实现

  • 创建项目
  • 将minizip.exe放到项目目录
  • 添加以下代码
    添加头文件
#include<iostream>
#include<Windows.h>

调用方法

int main() {
	std::string src_filePath("D:/material/project/data/miniTest2");
	std::string	dist_doc(".");
	std::string dist_file_name("compressed");

	std::string savepath = compress(src_filePath, dist_doc, dist_file_name);
	std::cout << "compressed file saved as " << savepath << std::endl;
}

compress函数实现

/*
* 压缩文件或文件夹,以zip为后缀
* srcpath:等待被压缩的文件
* directory:压缩后文件存储路径
*dist_filename:压缩后的文件名,不加zip
* 
*/
std::string  compress(const std::string& srcpath, const std::string& directory, const std::string& dist_filename) {
    
    std::string s_cmdPara = dist_filename + ".zip " + srcpath;
    WCHAR wc_exePath[] = L"minizip.exe";
    WCHAR wc_cmdPara[1024];
    WCHAR wc_directory[1024];
    swprintf(wc_cmdPara, 1024, L"%hs", s_cmdPara.c_str());
    swprintf(wc_directory, 1024, L"%hs", directory.c_str());
    //调用minizip.exe 压缩文件
    do_cmd(wc_exePath, wc_cmdPara, wc_directory);
    return s_cmdPara.substr(0, s_cmdPara.find(".zip")) + ".zip";
}

do_cmd函数实现

/*
* 执行命令
* exePath:执行文件
* cmdPara:参数
* directory:执行文件夹
*/
void do_cmd(const WCHAR exePath[], const WCHAR cmdPara[], const WCHAR directory[]) {
    SHELLEXECUTEINFO ShExecInfo = { 0 };
    ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
    ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
    ShExecInfo.hwnd = NULL;
    ShExecInfo.lpVerb = NULL;
    // exe的路径
    ShExecInfo.lpFile = exePath;
    ShExecInfo.lpParameters = cmdPara;/*your params*/
    ShExecInfo.lpDirectory = directory;/*WorkDirectory*/
    // 是否显示
    ShExecInfo.nShow = SW_HIDE;
    ShExecInfo.hInstApp = NULL;
    // 同步设置
    ShellExecuteEx(&ShExecInfo);
    WaitForSingleObject(ShExecInfo.hProcess, INFINITE);
}

排雷

下面的雷也不一定是真的雷,只是我没有成功使用以下方法压缩文件夹,要是有兄弟用下面的方法达到了文件压缩的目标,请多指教呀!

方法1

  • 参考链接:https://www.codeproject.com/Articles/7530/Zip-Utils-Clean-Elegant-Simple-Cplusplus-Win
  • 方法描述:Zip Utils,官网描述得很美,不用lib这些,直接在项目根目录添加源文件就可以使用。
  • 坑:按照官网指示,报错“未声明的标识符”“重定义”…
    在这里插入图片描述

方法2

  • 参考链接:https://blog.csdn.net/Struggling_Jeff/article/details/100857364
  • 方法描述:添加:zip.h,zip.cpp,unzip.h,unzip.cpp后编译,并使用博主提供的ZipHelper进行压缩
  • 踩坑:
    我的步骤是建立项目,添加要求的文件,建立空main,运行报错。

方法3

方法三不算踩坑,但只能压缩文件,不能压缩文件夹,是可以运行的。

  • 参考链接:https://www.zlib.net/
  • 方法描述:按参考链接教程所说,添加四个源文件,调用 compress()即可

方法4

调用minizip的接口

  • 参考链接:https://blog.51cto.com/hakuyo/1958102

自己的实现略了,找不到代码了,总之还是失败了,不能成功运行,一堆错误。

本来以为c++一定会有封装得漂漂亮亮的包,但是找了一圈都没找到,四处碰壁,最后用了菜办法,各位大佬要是有好用的包,请提点一下小的我(鞠躬)

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值