简体   繁体   中英

Extracting .rar/.zip using c++

For no particular reason, I'm currently working on a program that extracts .zip/.rar files using system() .

I currently have WinRar installed because winrar.exe is able to handle both .zip & .rar files.

int main()
{
    vector<wstring> files;

    if (ListFiles(L"folder", L"*", files))
    {
        string program = "\"C:\\Program Files\\WinRAR\\winrar.exe\"";
        string args = "x -y";
        string type = "*.*";

        TCHAR dir[MAX_PATH];
        GetCurrentDirectory(MAX_PATH, dir);
        wstring current_directory(wstring(L"\"") + dir + wstring(L"\\"));

        for (const auto& f : files)
        {
            if (wcscmp(PathFindExtension(f.c_str()), L".rar") == 0 ||
                wcscmp(PathFindExtension(f.c_str()), L".zip") == 0)
            {
                string file = ws2s(f.c_str());
                string output = "\"c:\\Users\\my name\\Desktop\\output\"";

                string command = program + " " + args + " " + ws2s(current_directory) + file + "\"" + " " + type + " " + output;
                cout << command << endl;

                if (system(command.c_str()) != 0)
                    return GetLastError();
            }
        }
    }

    return 0;
}

Because I'm using the command line, and don't want spaces to be a problem I wrap what I can in quotation marks:
-- "C:/users/username/program files (x86)/" --
-- "folder/zipped folder.zip" vs folder/"zipped folder.zip" --

After building the complete command contained in command , I printed it out to the screen so I could Edit->Mark:
"C:\\Program Files\\WinRAR\\winrar.exe" x -y "C:\\Users\\my name\\Documents\\Visual Studio 2013\\Projects\\extractor\\folder\\unzip.zip" *.* "c:\\Users\\my name\\Desktop\\output"

However, 'C:\\Program' is not recognized as an internal or external command, operable program or batch file. is what I'm met with after the system(command) call.

If I Copy & Paste the exact same command into Start->Command Prompt, it works like a dream.

How to extract ZIP files with WinRAR command line?
http://comptb.cects.com/using-the-winrar-command-line-tools-in-windows/
https://www.feralhosting.com/faq/view?question=36

Is there a way different way to invoke the system() call?
If there's not, how else can command line arguments be used?

I'd prefer to [avoid entirely] not use Boost:: or 3rd party libraries.

Thanks!

This is probably because of the quirky behavior of Command Prompt when it comes to quotation of arguments. Whenever you call system("\\"arg1\\" \\"arg2\\"") , it is equivalent to calling:

cmd.exe /c "arg1" "arg2"

Because of the strange behavior as described in the linked post, this will not be interpreted correctly by Command Prompt. An extra set of quotes is needed:

cmd.exe /c ""arg1" "arg2""

For invoking executables, CreateProcess provides an alternative that gives you more control over the process. You'll still have to quote the arguments but the rules are a bit simpler as the Command Prompt is no longer in your way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM