7z命令行来压缩文件夹

Pac*_*art 4 c# 7zip

我正在尝试使用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();
Run Code Online (Sandbox Code Playgroud)

Mar*_*rco 10

如评论部分所述,您应该使用 7za.exe

链接为您提供了完整的示例行

您的代码将如下所示:

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();
Run Code Online (Sandbox Code Playgroud)


dig*_*All 5

gzip以及bzip2唯一的压缩算法,并且不能被用于压缩的一个文件系统结构(例如文件夹,与文件等文件夹).

实际上,它们通常先于tar压缩(支持文件夹),以获得着名的(特别是在基于unix的系统中)tar.gztar.bz2归档.

在您的情况下,您可以使用-tzip-t7zip直接压缩文件夹:

p.Arguments = "a -t7z \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
Run Code Online (Sandbox Code Playgroud)

顺便说一下,你应该使用7za.exe而不是7zG.exe因为后者是GUI模块,而前者是7zip的命令行独立版本(即它不依赖于任何其他dll),如7zip手册中所述:

7z.exe是7-Zip的命令行版本.7z.exe使用7-Zip包中的7z.dll.7-Zip文件管理器也使用7z.dll.

7za.exe(a = alone)是7-Zip的独立版本.7za.exe仅支持7z,lzma,cab,zip,gzip,bzip2,Z和tar格式.7za.exe不使用外部模块.

您可以7za.exe在额外的软件包中找到,例如版本9.22,您可以在名为7z922_extra.7z(链接)的存档中找到它.