5

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();
digEmAll
  • 56,430
  • 9
  • 115
  • 140
Pacobart
  • 241
  • 2
  • 7
  • 20
  • Why are you using `7zG`? Shouldn't you use [`7za`](http://www.dotnetperls.com/7-zip-examples), the command line only version? – gunr2171 Oct 09 '13 at 20:33
  • 3
    `-tgzip` doesn't support folders, use `-t7z` instead (i.e. 7zip format) or other formats (e.g. -tzip)... – digEmAll Oct 09 '13 at 20:34
  • Or add an asterix at the end of your path as explained here. http://stackoverflow.com/questions/10753667/compressing-only-files-using-7z-without-preserving-the-path – priehl Oct 09 '13 at 20:40
  • what is the difference between 7zG and 7za? – Pacobart Oct 09 '13 at 21:04
  • 1
    `7zG.exe` is the GUI module of 7zip, while `7z.exe` is the command line version. The `7za.exe` is the standalone version of `7z.exe` (basically it does not depend on any dll, like `7z.dll`) – digEmAll Oct 09 '13 at 21:19
  • I am using 7zG.exe and there isn't a GUI for it. But, it does require 7z.dll. is 7za.exe 64bit compatible? I read somewhere that 7zG.exe was. – Pacobart Oct 09 '13 at 21:28
  • Try to run 7zG.exe from the command prompt with wrong arguments, you will get a modal dialog (i.e. a GUI module). You can find `7za.exe` in the extra package (e.g. [for version 9.22 is in the archive called 7z922_extra.7z](http://sourceforge.net/projects/sevenzip/files/7-Zip/9.22/) ). I think it is compiled for x86 so it's completely compatible with both x86 and x64 systems. – digEmAll Oct 09 '13 at 21:32

3 Answers3

10

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();
gunr2171
  • 16,104
  • 25
  • 61
  • 88
Marco
  • 22,856
  • 9
  • 75
  • 124
5

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

digEmAll
  • 56,430
  • 9
  • 115
  • 140
  • Hi digEmAll, this is a really nice solution. Do you further know, how this job can be done via `7z.dll` without the `7za.exe`? – Bastian Ebeling Aug 28 '14 at 13:08
  • 1
    @BastianEbeling: yes you can, but 7z.dll is an unmanaged dll (written in c++ I think) and you need to create a wrapper for that. There are some examples and already written libraries (e.g. [this one](http://sevenzipsharp.codeplex.com/)) but if you look [here](http://www.7-zip.org/sdk.html) in the download package there should be a .cs file implementing in C# the algorithm for 7z compression... – digEmAll Aug 28 '14 at 13:33
  • Hi @dimEmAll, so sda - I can only find a function for compression one Stream - not for building an archive from a bunch of files. – Bastian Ebeling Aug 28 '14 at 13:56
  • @BastianEbeling: did you take a look to this [LINK](http://sevenzipsharp.codeplex.com/) ? – digEmAll Aug 28 '14 at 15:56
0

try with this command:

7za  -tzip <archive-name> <folder-name>
Guillermo Zacur
  • 321
  • 2
  • 5