通过 c++ 中的 system() 使用 7-zip
我正在尝试使用 7-Zip 在 Windows XP 计算机上通过 C++ 中的 system()
函数来压缩文件。 我尝试过:(
格式化为 system() 收到的格式)
"C:\Program Files\7-Zip\7z.exe" a -tzip "bleh.zip" "addedFile.txt"
抛出错误
'C:\Program' 未被识别为内部或外部命令, 可操作的程序或批处理文件。
我已经尝试了几种类似的替代方案,但尚未找到解决方案。
我想尝试直接从安装目录运行它,这样只要用户安装了 7-Zip,它就能够运行。这是用于内部实用程序应用程序。
编辑: 根据要求,这些是实际的代码行:
std::string systemString = "\"C:\\Program Files\\7-Zip\\7z.exe\" a -tzip \"" + outDir + projectName + ".zip" + "\" \"";
//...
std::string finalSystemString = systemString + *i + "\"";
system( finalSystemString.c_str() );
*i
是要添加的特定文件的迭代器。
I'm trying to use 7-Zip to zip up a file via the system()
function in C++ on a windows XP machine.
I tried:
(formatted to be what system() would have received)
"C:\Program Files\7-Zip\7z.exe" a -tzip "bleh.zip" "addedFile.txt"
which spat the error
'C:\Program' is not recognized as an internal or external command,
operable program or batch file.
I've tried several similar alternatives but have not yet found a solution.
I want to try to run it straight from its install directory so that as long as a user has 7-Zip installed it will be able to function. This is for an in house utility application.
EDIT:
as requested these are the actual lines of code:
std::string systemString = "\"C:\\Program Files\\7-Zip\\7z.exe\" a -tzip \"" + outDir + projectName + ".zip" + "\" \"";
//...
std::string finalSystemString = systemString + *i + "\"";
system( finalSystemString.c_str() );
*i
is an iterator to a particular file that is getting added.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
看起来有些东西正在剥离第一个参数周围的引号。您可以使用额外的引号来尝试解决此问题,或者您可以使用 Win32 API GetShortPathName
短路径中不会有空格,它将类似于“C:\PROGRA~1\7- ZIP\7Z.EXE”
it looks like something is stripping the quotes around the first argument. You could play around with extra quotes to try and fix this, or you can get the MS-DOS compatible short path name for 7z.exe with the Win32 API GetShortPathName
The short path will not have spaces in it, it will be something like "C:\PROGRA~1\7-ZIP\7Z.EXE"
您是否尝试过转义空格,即“C:\Program\ Files\7-Zip\7z.exe”?尽管我不知道 system() 的具体细节,但这可能会起作用。
Have you tried escaping the spaces, i.e. "C:\Program\ Files\7-Zip\7z.exe"? That might work, although I don't know the specifics of system().
另一种方法是使用 CreateProcess Windows API 中的函数。根据其文档,它可以处理“C:\Program Files”中的空格。
Another approach would be to use the CreateProcess function in the Windows API. It can deal with spaces in "C:\Program Files" according to its documentation.