C# 7z 命令行压缩文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19281957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
7z command line to compress folders
提问by Pacobart
I am trying to zip/7z folders using the command line of 7zG.exe. The code I have works for files but not folders. Could someone please show me the correct way using 7z command line to compress folders? Here is the sample code that works for files only. Whenever I try running this code 7zip shows a messagebox saying "Invalid Parameter"
我正在尝试使用 7zG.exe 的命令行压缩/7z 文件夹。我的代码适用于文件,但不适用于文件夹。有人可以告诉我使用 7z 命令行压缩文件夹的正确方法吗?这是仅适用于文件的示例代码。每当我尝试运行此代码时,7zip 都会显示一个消息框,上面写着“参数无效”
string sourceName = "Folder\Folder1";
string targetName = "Example.gz";
// 1
// Initialize process information.
//
ProcessStartInfo p = new ProcessStartInfo();
p.FileName = "7zG.exe";
// 2
// Use 7-zip
// specify a=archive and -tgzip=gzip
// and then target file in quotes followed by source file in quotes
//
p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
// 3.
// Start process and wait for it to exit
//
Process x = Process.Start(p);
x.WaitForExit();
采纳答案by Marco
as stated in to comment section, you are supposed to use 7za.exe
如评论部分所述,您应该使用 7za.exe
This linkgives you a complete example line
此链接为您提供完整的示例行
Your code will look like this:
您的代码将如下所示:
string sourceName = "Folder\Folder1";
string targetName = "Example.gz";
ProcessStartInfo p = new ProcessStartInfo();
//first change
p.FileName = "7za.exe";
//second change
p.Arguments = "a -tzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
p.WindowStyle = ProcessWindowStyle.Hidden;
Process x = Process.Start(p);
x.WaitForExit();
回答by Guillermo Zacur
try with this command:
试试这个命令:
7za -tzip <archive-name> <folder-name>
回答by digEmAll
gzip
as well as bzip2
are onlycompression algorithms and cannot be used to compress a file-system structure (e.g. folders, folders with files etc.).
gzip
以及bzip2
是唯一的压缩算法,并且不能被用于压缩的一个文件系统结构(例如文件夹,与文件的文件夹等)。
In fact, they are usually preceded by tar
compression (that support folders), to get the famous (in particular in unix-based systems) tar.gz
and tar.bz2
archives.
事实上,它们之前通常是tar
压缩(即支持文件夹),以获得著名的(特别是在基于 unix 的系统中)tar.gz
和tar.bz2
存档。
In your case you can use -tzip
or -t7zip
to directly compress a folder:
在您的情况下,您可以使用-tzip
或-t7zip
直接压缩文件夹:
p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
By the way, you should use 7za.exe
instead of 7zG.exe
since the latter is the GUI module, while the former is the command-line standalone version of 7zip (i.e. it does not depend on any other dll), as stated in 7zip manual:
顺便说一句,您应该使用7za.exe
而不是7zG.exe
因为后者是 GUI 模块,而前者是 7zip 的命令行独立版本(即它不依赖于任何其他 dll),如 7zip 手册中所述:
7z.exe is the command line version of 7-Zip. 7z.exe uses 7z.dll from the 7-Zip package. 7z.dll is used by the 7-Zip File Manager also.
7za.exe (a = alone) is a standalone version of 7-Zip. 7za.exe supports only 7z, lzma, cab, zip, gzip, bzip2, Z and tar formats. 7za.exe doesn't use external modules.
7z.exe 是 7-Zip 的命令行版本。7z.exe 使用 7-Zip 包中的 7z.dll。7z.dll 也被 7-Zip 文件管理器使用。
7za.exe(a = 单独)是 7-Zip 的独立版本。7za.exe 仅支持 7z、lzma、cab、zip、gzip、bzip2、Z 和 tar 格式。7za.exe 不使用外部模块。
You can find 7za.exe
in the extra package, for example for version 9.22 you can find it in the archive called 7z922_extra.7z
(link).
您可以7za.exe
在额外的包中找到,例如对于 9.22 版,您可以在名为7z922_extra.7z
( link)的存档中找到它。