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"
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();
gzip
as well asbzip2
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
andtar.bz2
archives.In your case you can use
-tzip
or-t7zip
to directly compress a folder:By the way, you should use
7za.exe
instead of7zG.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:You can find
7za.exe
in the extra package, for example for version 9.22 you can find it in the archive called7z922_extra.7z
(link).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:
try with this command: