使用 C++ 提取 .rar/.zip
Extracting .rar/.zip using c++
没有特别的原因,我目前正在开发一个使用 system()
.
提取 .zip/.rar 文件的程序
我目前安装了 WinRar,因为 winrar.exe 能够处理 .zip 和 .rar 文件。
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;
}
因为我正在使用命令行,并且不希望空格成为问题,所以我将我能用的引号括起来:
-- "C:/users/username/program files (x86)/"
--
-- "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)
电话后遇到的情况。
如果我将完全相同的命令复制并粘贴到“开始”->“命令提示符”中,它就像梦一样。
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
是否有不同的方法来调用 system()
调用?
如果没有,还可以如何使用命令行参数?
我宁愿[完全避免]不使用 Boost:: 或第 3 方库。
谢谢!
这可能是因为 quirky behavior of Command Prompt 在引用参数时。每当调用system("\"arg1\" \"arg2\"")
,就相当于调用:
cmd.exe /c "arg1" "arg2"
由于链接 post 中描述的奇怪行为,命令提示符无法正确解释。需要一组额外的引号:
cmd.exe /c ""arg1" "arg2""
对于调用可执行文件,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 因为命令提示符不再妨碍您。
没有特别的原因,我目前正在开发一个使用 system()
.
我目前安装了 WinRar,因为 winrar.exe 能够处理 .zip 和 .rar 文件。
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;
}
因为我正在使用命令行,并且不希望空格成为问题,所以我将我能用的引号括起来:
-- "C:/users/username/program files (x86)/"
--
-- "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)
电话后遇到的情况。
如果我将完全相同的命令复制并粘贴到“开始”->“命令提示符”中,它就像梦一样。
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
是否有不同的方法来调用 system()
调用?
如果没有,还可以如何使用命令行参数?
我宁愿[完全避免]不使用 Boost:: 或第 3 方库。
谢谢!
这可能是因为 quirky behavior of Command Prompt 在引用参数时。每当调用system("\"arg1\" \"arg2\"")
,就相当于调用:
cmd.exe /c "arg1" "arg2"
由于链接 post 中描述的奇怪行为,命令提示符无法正确解释。需要一组额外的引号:
cmd.exe /c ""arg1" "arg2""
对于调用可执行文件,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 因为命令提示符不再妨碍您。