7z程序
准备
-
下载7-zip
官方网址:https://sparanoid.com/lab/7z/
-
知识储备
a - 压缩;x ,e- 解压
-
e命令解压的时候不会保持目录结构,会把所有的文件都解压到目的目录下,不管这些文件是不是位于子文件夹。
-
x命令则会保存目录结构
转义字符需要加入,例如\ "
-
编程
-
命令行
- 参数说明
- 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; }
特殊路径处理
-
带有空格的路径
带有空格的通配符或文件名必须加上引号: "Dir\Program files\*" Dir\"Program files"\*