利用7z程序压缩、解压

7z程序

准备

  1. 下载7-zip

    官方网址:https://sparanoid.com/lab/7z/

  2. 知识储备

    a - 压缩;x ,e- 解压

    • e命令解压的时候不会保持目录结构,会把所有的文件都解压到目的目录下,不管这些文件是不是位于子文件夹。

    • x命令则会保存目录结构

    转义字符需要加入,例如\ "

编程

  1. 命令行

    • 参数说明
    • nCompressType:0 压缩;1 解压
    • exePath:7z.exe路径
    • srcPath:当nCompressType为0的时候,文件夹路径;当nCompressType为1的时候,压缩包路径
    • destPath:当nCompressType为0的时候,压缩包路径;当nCompressType为1的时候,文件夹路径

    压缩例子:

    //命令行中存在空格的处理 ,加入""
     	std::string strExePath = "D:\\7-Zip\\7z.exe";
     	std::string  srcPath = "D:\\xx  xx(xx)";
     	std::string destPath = "D:\\xx  xx(xx)";
     	bool bRes = zipCompress(strExePath, "\"" + srcPath + "\"\\" + "\*", "\"" + destPath + "\".zip", 0);
    
    bool zipCompress(std::string exePath, std::string srcPath, std::string destPath, int nCompressType)
    {
        char cmd[1000] = { 0 };
        if (nCompressType == 0)
        {
            snprintf(cmd, sizeof(cmd), "%s a %s %s", exePath.c_str(), destPath.c_str(), srcPath.c_str());
        }
        else if (nCompressType == 1)
        {
            snprintf(cmd, sizeof(cmd), "%s x %s -o%s", exePath.c_str(), srcPath.c_str(), destPath.c_str());
        }
        logger_write(Logger::Logger_Level_DEBUG,"the cmd is %s",cmd);
        system(cmd);
        return true;
    }
    

特殊路径处理

  1. 带有空格的路径

    带有空格的通配符或文件名必须加上引号: 
    "Dir\Program files\*" 
     Dir\"Program files"\* 
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气转身而过

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值