Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

extract 7zip in C# code

Tags:

c#

.net

7zip

I need use 7zip in C#. Without console, just with 7zSharp.dll ? + I find some data here

http://7zsharp.codeplex.com/releases/view/10305 ,

but I don't know how to use it( - I could create .bat(.cmd) file, but I need throught dll file) Exactly: I need extract .7z file with key)

like image 831
koder_mooder Avatar asked Nov 03 '11 12:11

koder_mooder


People also ask

How do I extract here using 7-Zip?

Extracting 7zip files Locate the desired zip file. Right-click it and select 7-Zip > Extract Here. The file will be extracted to the folder containing the zipped file. Select Extract files to change the extraction folder.

Is 7-Zip better than WinRAR?

While both are compression programs, 7-Zip can compress files into a wider range of format types, including 7z. WinRAR can only compress into RAR or ZIP formats. However, both can decompress a wide variety of format types. Also, 7-Zip is open source and free, while WinRAR costs over $30 for a lifetime license.

Can WinRAR open 7z?

WinRAR can open 7Z (7-Zip) extension by default.

How do I unzip a zip file?

To unzip a single file or folder, open the zipped folder, then drag the file or folder from the zipped folder to a new location. To unzip all the contents of the zipped folder, press and hold (or right-click) the folder, select Extract All, and then follow the instructions.


1 Answers

Download the standalone console version from 7zip.com and add it to your project.

You need those 3 Files added in the project:

  1. 7za.exe
  2. 7za.dll
  3. 7zxa.dll

Don't forget to say Copy to Output Directory in it's preferences.

Extract an archive:

public void ExtractFile(string sourceArchive, string destination)
    {
        string zPath = "7za.exe"; //add to proj and set CopyToOuputDir
        try
        {
            ProcessStartInfo pro = new ProcessStartInfo();
            pro.WindowStyle = ProcessWindowStyle.Hidden;
            pro.FileName = zPath;
            pro.Arguments = string.Format("x \"{0}\" -y -o\"{1}\"", sourceArchive, destination);
            Process x = Process.Start(pro);
            x.WaitForExit();
        }
        catch (System.Exception Ex) {
            //handle error
        }
    }

Create an archive:

public void CreateZip(string sourceName, string targetArchive)
{
    ProcessStartInfo p = new ProcessStartInfo();
    p.FileName = "7za.exe";
    p.Arguments = string.Format("a -tgzip \"{0}\" \"{1}\" -mx=9", targetArchive, sourceName);
    p.WindowStyle = ProcessWindowStyle.Hidden;
    Process x = Process.Start(p);
    x.WaitForExit();
}
like image 152
Vishal Sen Avatar answered Sep 20 '22 17:09

Vishal Sen