Project Description

SevenZipLib is a lightweight, easy-to-use managed interface to the 7-zip library.

Supported formats

  • Packing / unpacking: 7z, ZIP, GZIP, BZIP2, TAR, XZ
  • Unpacking only: ARJ, CAB, CHM, CPIO, DEB, DMG, HFS, ISO, LZH, LZMA, MSI, NSIS, RAR, RPM, UDF, WIM, XAR, Z, XZ, and more.

Example Usage

Here are some examples in C# illustrating how to use the library to read, extract or create archives.

List files

using (SevenZipArchive archive = new SevenZipArchive("Sample.rar"))
{
    foreach (ArchiveEntry entry in archive)
    {
        Console.WriteLine(entry.FileName);
    }
}

List files in an encrypted archive

using (SevenZipArchive archive = new SevenZipArchive("EncryptedArchive.rar", ArchiveFormat.Unknown, Password))
{
    foreach (ArchiveEntry entry in archive)
    {
        Console.WriteLine(entry.FileName);
    }
}

Perform an integrity check

using (SevenZipArchive archive = new SevenZipArchive("Sample.zip"))
{
    bool checksOK = archive.CheckAll();
    Console.WriteLine(checksOK ? "Archive is OK." : "Archive is not OK");
}

Extract entries to a directory

using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
    archive.ExtractAll(TargetDirectory);
}

Extract entries to a directory (alternative)

using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
    foreach (ArchiveEntry entry in archive)
    {
        entry.Extract(TargetDirectory);
        // You can also use archive.Extract(entry.FileName, TargetDirectory)
    }
}

Extract with overwrite

using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
    archive.ExtractAll(TargetDirectory, ExtractOptions.OverwriteExistingFiles);
}

Use LINQ to selectively extract files

using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
    var entries =
        from entry in archive
        where !entry.IsDirectory && entry.FileName.EndsWith(".txt");
        select entry;
    entries.ExtractAll(TargetDirectory);
}

Use LINQ to selectively extract files (alternative)

using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
    archive.Where(entry => !entry.IsDirectory && entry.FileName.EndsWith(".txt")).ExtractAll(TargetDirectory);
}

Extract into a stream

using (SevenZipArchive archive = new SevenZipArchive("Sample.7z"))
{
    archive.Extract("File.txt", OutputStream);
    // You can also use archive["MyFile.txt"].Extract(OutputStream);
}

Extract an encrypted entry

using (SevenZipArchive archive = new SevenZipArchive("Sample.zip"))
{
    ArchiveEntry entry = archive["EncryptedFIle.txt"];
    entry.Extract(TargetDirectory, ExtractOptions.None, Password);
}

Last edited Sep 5, 2010 at 9:10 PM by gael, version 11