Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

7z command line to compress folders

Tags:

c#

7zip

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();
like image 325
Pacobart Avatar asked Oct 09 '13 20:10

Pacobart


People also ask

How do I use 7Zip command-line?

Invoke the version of 7Zip you are using by entering "7z" for P7Zip (7z.exe), or "7za" for 7Zip for Windows (7za.exe) to start either the P7-Zip or 7za application prior to entering commands. Other than this program invocation command, all commands, parameters and switches are identical for all command-line versions.

Can you compress a 7z file?

7-Zip is a free and open-source file archiver for compressing and uncompressing files.


1 Answers

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();
like image 169
Marco Avatar answered Sep 27 '22 23:09

Marco