7z command line to compress folders

时光总嘲笑我的痴心妄想 提交于 2019-12-04 10:33:45
Marco

as stated in to comment section, you are supposed to use 7za.exe

This link gives 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();

gzip as well as bzip2 are only compression algorithms and cannot be used to compress a file-system structure (e.g. folders, folders with files etc.).

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.

In your case you can use -tzip or -t7zip to directly compress a folder:

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:

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.

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).

try with this command:

7za  -tzip <archive-name> <folder-name>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!