0xC0DEFACE
0xC0DEFACE

Reputation: 9211

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

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.

Upvotes: 1

Views: 5566

Answers (3)

user153062
user153062

Reputation:

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.

Upvotes: 0

John Knoeller
John Knoeller

Reputation: 34158

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"

Upvotes: 4

Ibrahim
Ibrahim

Reputation: 1903

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

Upvotes: 3

Related Questions