C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Compression results:
Standard .NET GZIP: 895,425 bytes (Uses GZipStream class)
7-Zip GZIP: 825,285 bytes
7-Zip GZIP Ultra: 819,631 bytes
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.
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.
ProcessWarning: 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();
}
}
Note: The first argument (a) in the command doesn't have a hyphen (-) before it.