Using 7-zip via system() in c++

情到浓时终转凉″ 提交于 2019-12-08 08:21:23

问题


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.


回答1:


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"




回答2:


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().




回答3:


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.



来源:https://stackoverflow.com/questions/2227038/using-7-zip-via-system-in-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!