All formats

SevenZipExtractor

LanguageC# (.NET Standard 2.0, .NET Framework 4.5)
LicenseMIT (the wrapper); the bundled 7z.dll is under the 7-Zip license (LGPL with an unRAR restriction)
Repositorygithub.com/adoconnection/SevenZipExtractor
DocumentationREADME and wiki
Depends on7z.dll (x86 and x64 copies are included in the NuGet package)
Read / writeyes / no; everything 7-Zip reads, including AES-encrypted archives

SevenZipExtractor calls the native 7-Zip library through COM interfaces. Because 7-Zip itself decodes the data, it opens every 7z variant (LZMA2, PPMd, BCJ2, AES-256, encrypted headers) at native speed, and also RAR, RAR5, ZIP, TAR, ISO, CAB, DMG, WIM and about 40 other formats. The format is detected from the file contents, so files without an extension work too.

It only extracts; it can’t create or modify archives. It runs on Windows only, because it loads 7z.dll. For cross-platform code or for creating archives use SharpCompress.

Installation#

Shell
dotnet add package SevenZipExtractor

7z.dll for x86 and x64 is copied to your output folder automatically.

Basic usage: extract a 7z archive#

C#
using SevenZipExtractor;

using (ArchiveFile archiveFile = new ArchiveFile("archive.7z"))
{
    foreach (Entry entry in archiveFile.Entries)
    {
        Console.WriteLine($"{entry.FileName}  {entry.Size} bytes  {entry.Method}");
    }
    archiveFile.Extract("output", overwrite: true);  // extract everything
}

How to extract a password-protected 7z archive#

C#
using (ArchiveFile archiveFile = new ArchiveFile("secret.7z"))
{
    archiveFile.Extract("output", overwrite: true, password: "mySecret");
}

How to extract selected files from a solid archive#

Calling entry.Extract for each file in a solid 7z archive decompresses the block from the start every time. Pass a callback instead: the archive is read once and the callback decides, for each entry, where to write it or null to skip it.

C#
using (ArchiveFile archiveFile = new ArchiveFile("archive.7z"))
{
    archiveFile.Extract(entry =>
    {
        if (entry.FileName.EndsWith(".pdf", StringComparison.OrdinalIgnoreCase))
            return Path.Combine("output", entry.FileName);
        return null;  // skip
    });
}

How to read a file from a 7z archive into memory#

C#
using (ArchiveFile archiveFile = new ArchiveFile("archive.7z"))
{
    Entry entry = archiveFile.Entries.First(e => e.FileName == "config.json");
    using var memoryStream = new MemoryStream();
    entry.Extract(memoryStream);
    string json = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray());
}

How to open an archive from a stream#

C#
using (FileStream stream = File.OpenRead("download.bin"))
using (ArchiveFile archiveFile = new ArchiveFile(stream, SevenZipFormat.SevenZip))
{
    archiveFile.Extract("output");
}

Omit the format to let 7-Zip detect it.

Tips#