9

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)

koder_mooder
  • 124
  • 1
  • 2
  • 9

5 Answers5

21

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();
}
CodingYourLife
  • 7,172
  • 5
  • 55
  • 69
Vishal Sen
  • 1,135
  • 1
  • 13
  • 23
  • Hi, how to include the password in the Arguments to create zipped and extract zipped with the solution above? – hunterex Aug 21 '18 at 12:37
  • @hunterex the argument for passwords is "-p" so it's -pMySuperS3cr3tPassword – Uke Jun 08 '20 at 13:29
  • 1
    In order for ProcessWindowStyle to work, you also need to have UseShellExecute set to true –  Jun 09 '21 at 10:06
  • Note that `Process` implements `IDisposable`, so don't forget to wrap those last two lines into a `using` statement – taiji123 Oct 01 '21 at 13:16
6

The authors of 7zip provide the LZMA SDK and good documentation which should be able to achieve what you want. The SDK includes C# code capable of compression / decompression.

Another option would be to use a something like C# (.NET) Interface for 7-Zip Archive DLLs

UPDATE: Another user asked a similar question here: How do I create 7-Zip archives with .NET? The answer has several of the same links I provided and a few more.

Community
  • 1
  • 1
robowahoo
  • 1,259
  • 9
  • 10
  • Note that the .NET LZMA SDK code does not provide for streaming compression and decompression, so working with large files without writing them to disk is very difficult. The main function is basically something like this: `void Code(Stream in, Stream out)`. Reading a compressed file and processing it as you would require writing a `Stream` class that did the processing as the writes came in. Wrapper streams like `new Decompressingwrapper(compressedInput)` and `new CompressingWrapper(decompressedInput) would make it MUCH easier to use in most scenarios. – James Mar 13 '18 at 22:45
0

Aspose.ZIP can be used to extract zip files with ease. Download it via NuGet package manager.

PM> Install-Package Aspose.Zip

The following code shows how to open or extract 7z file programmatically using C#:

using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
    archive.ExtractToDirectory(dataDir + "Sample_ExtractionFolder");
}

The code below explains how to extract or unzip a password protected 7zip file programmatically using C#:

using (SevenZipArchive archive = new SevenZipArchive("Sample_Encrypted.7z"))
{
    archive.ExtractToDirectory("Sample_Encrypted7zip", "password");
}

Credit to Farhan Raza's blog post: https://blog.aspose.com/2021/04/28/open-extract-7zip-7z-file-unzip-in-csharp-asp-net/

Péter Szilvási
  • 362
  • 4
  • 17
0

It doesn't look like this library supports encrypted files. No method takes a key as a parameter.

Christian Wattengård
  • 5,543
  • 5
  • 30
  • 43
0

The 7zSharp library doesn't seem to support password as input, just a zip file.

The library just calls the .exe of 7zip, so you could donwload the source and alter it to accept a password parameter which you then pass to the executable.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272