TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# 7 Zip Executable (Process.Start)

Invoke the 7-Zip executable program with the Process.Start method. Run 7za.exe as a Process.
7-Zip can be used in C# programs. It provides excellent compression ratios. We embed the 7-Zip command-line executable in a C# program. We then invoke it with the Process class. This code can easily be reused.7-Zip Command-Line
First, you need to create a new C# project, usually either Console or Windows Forms, although any will work. Here we will show a console application. Next, you need to download the executable.
Compression results: Standard .NET GZIP: 895,425 bytes (Uses GZipStream class) 7-Zip GZIP: 825,285 bytes 7-Zip GZIP Ultra: 819,631 bytes
Go to the downloads page at 7-Zip.org and download the console application. The description is "7-Zip Command Line Version" and the filename is 7za457.zip. This is the file we will embed in our program. After you download, unzip it.Download: 7-zip.org
Add 7za.exe. Right-click on your project name in the Solution Explorer and select Add Existing Item. You need to change the drop-down icon on the dialog to "All Files (*.*)", which will show the 7za.exe executable.
Copy if newer. With Visual Studio, you must specify "Copy if newer" or "Copy always" to copy files such as executables to the output directory. Specify either of those options to copy the 7za.exe executable.

Add: We need an example file to test 7-Zip with, so add a new text file to your console project.

Tip: Make sure to specify "Copy if newer" for it too. This will be our source file.

Implementation. In the first part of the code, we specify the source file name and the target file name. The source file name is the name of the text file you added to the project. The target name is the location of the archive you want to create.

Note: If the archive is already there, you will have to delete it or tell 7-Zip to overwrite it.

ProcessStartInfo: We set the FileName to the name of the 7za.exe executable in the project. Pay close attention to the quotes in Arguments.

Info: In this example, I use GZip compression. We actually start the Process and execute it.

Process

Warning: The Windows OS can cause problems here if you are not an administrator on your PC.

C# program that calls 7-Zip executable using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Diagnostics; class Program { static void Main() { string sourceName = "ExampleText.txt"; string targetName = "Example.gz"; // 1 // Initialize process information. // ProcessStartInfo p = new ProcessStartInfo(); p.FileName = "7za.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(); } }
Verify results. Now we need to verify results. Open the bin\Debug or bin\Release folder in your project's directory, and you should see the compressed file. Extract that file with 7-Zip in Windows, and it will have the same data.
First make sure all the files are being copied and the Arguments syntax is correct. The quotes in the syntax are really important if you deal with more complicated paths. After that, you might have problems with Vista's UAC stuff.

Note: The first argument (a) in the command doesn't have a hyphen (-) before it.

Options. We cover only some options. 7-Zip itself provides an excellent reference. Go to the 7-Zip folder and double-click on the "7-zip.chm" file. That's a compiled HTML help file that you can browse for many options.
Specify compression levels. You want the smallest possible compressed files, but don't want to wait for hours. On the example, I use -mx=9, which sets the maximum for Zip and GZip. Experiment with this.
Specify archive type. You may need something different from GZip in your program. The ".7z" format probably has the best compression ratio. If you want to use HTTP compression, use GZip, but if you are archiving, then I suggest 7z.
Summary. We invoked the 7-Zip executable from C# code. Sometimes it is best to go outside the .NET Framework when developing and use an open-source exe like 7-Zip. For me, 10% is a big improvement and 7-Zip is definitely worthwhile.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf