首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用c++提取..rar/..zip

使用c++提取..rar/..zip
EN

Stack Overflow用户
提问于 2015-03-20 07:17:35
回答 1查看 3.2K关注 0票数 0

没有什么特别的原因,我目前正在开发一个使用system()提取..zip/..rar文件的程序。

我现在已经安装了WinRar,因为winrar.exe能够同时处理.zip和.rar文件。

代码语言:javascript
运行
复制
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;
}

因为我使用的是命令行,而且不希望空格成为问题,所以我尽可能用引号包装:

代码语言:javascript
运行
复制
  -- `"C:/users/username/program files (x86)/"` --
代码语言:javascript
运行
复制
  -- `"folder/zipped folder.zip" vs folder/"zipped folder.zip"` --

在构建了包含在command中的完整命令之后,我将其打印到屏幕上,以便编辑->标记:

"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"

然而,'C:\Program' is not recognized as an internal or external command, operable program or batch file.是我在system(command)调用之后遇到的。

如果我将完全相同的命令复制并粘贴到Start->Command提示符中,它就会像梦一样工作。

如何使用WinRAR命令行提取ZIP文件?

http://comptb.cects.com/using-the-winrar-command-line-tools-in-windows/

https://www.feralhosting.com/faq/view?question=36

是否有不同的方式来调用system()调用?

如果没有,那么如何使用命令行参数?

我宁愿完全避免使用Boost::或第三方库。

谢谢!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-20 19:48:56

这可能是因为在引用参数时使用了命令提示符的古怪行为。无论何时调用system("\"arg1\" \"arg2\""),都等同于调用:

代码语言:javascript
运行
复制
cmd.exe /c "arg1" "arg2"

由于链接post中描述的奇怪行为,命令提示符无法正确解释这一点。还需要一套额外的引号:

代码语言:javascript
运行
复制
cmd.exe /c ""arg1" "arg2""

对于调用可执行文件,CreateProcess提供了一种替代方法,使您能够更好地控制进程。您仍然需要引用参数,但是作为命令提示符的规则更简单一些不再妨碍您。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/29161340

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档