C # .net code sample for zip file using 7zip

I installed 7-zip 4.65 on my machine in C: \ Program files. I want to use it in C # code for a zip file. The file name will be provided dynamically by the user. Can someone provide some sample code on how to use 7zip in C # code?

+4
source share
5 answers

Instead of the binary version, you need the source code.

This can be obtained as the LZMA SDK .

There you will find a folder CSthat contains the implementation of the C # algorithm for 7zip files.

+4
source

many answers above, but I used this below, mentioning the code to honor or unzip the file using 7zip

7zip .

     public void ExtractFile(string source, string destination)
        {
            // If the directory doesn't exist, create it.
            if (!Directory.Exists(destination))
                Directory.CreateDirectory(destination);

            string zPath = @"C:\Program Files\7-Zip\7zG.exe";
// change the path and give yours 
            try
            {
                ProcessStartInfo pro = new ProcessStartInfo();
                pro.WindowStyle = ProcessWindowStyle.Hidden;
                pro.FileName = zPath;
                pro.Arguments = "x \"" + source + "\" -o" + destination;
                Process x = Process.Start(pro);
                x.WaitForExit();
            }
            catch (System.Exception Ex) {
              //DO logic here 
              }
        }

zip

public void CreateZip()
{
    string sourceName = @"d:\a\example.txt";
    string targetName = @"d:\a\123.zip";
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = @"C:\Program Files\7-Zip\7zG.exe";
    p.Arguments = "a -tgzip \"" + targetName + "\" \"" + sourceName + "\" -mx=9";
    p.WindowStyle = ProcessWindowStyle.Hidden;
    Process x = Process.Start(p);
    x.WaitForExit();
}
+4

Source: https://habr.com/ru/post/1753703/


All Articles