---
title: "Extract a 7z file in C#"
url: https://recursiveai.org/onearchive/cookbook/extract-7z/
description: "Extract a 7z file in C#: open a .7z archive, list its entries and extract it to a folder, password-protected archives included. Pure C#, no 7-Zip install or native library."
version: "1.0.0"
---

# Extract a 7z file in C#

Call `Archive.Extract` with the `.7z` file and a target folder. The program below lists and extracts the 7-Zip source code, a real 7z archive you can download to try it.

```shell
dotnet new console -o Extract7z && cd Extract7z
dotnet add package RecursiveAI.OneArchive --version 1.0.0
curl -LO https://github.com/ip7z/7zip/releases/download/24.09/7z2409-src.7z
```

Put this in `Program.cs` and run `dotnet run`:

```csharp
using RecursiveAI.OneArchive;

// List what is inside.
using (var archive = Archive.Open("7z2409-src.7z"))
{
    var files = archive.Entries.Where(e => e.Kind == ArchiveEntryKind.File).ToList();
    Console.WriteLine($"{files.Count} files, {files.Sum(e => e.Size):N0} bytes");
    foreach (var e in files.Take(5))
        Console.WriteLine($"  {e.Size,9:N0}  {e.Name}");
}

// Extract everything into a folder.
Archive.Extract("7z2409-src.7z", "7zip-src");
int count = Directory.GetFiles("7zip-src", "*", SearchOption.AllDirectories).Length;
Console.WriteLine($"extracted {count} files to 7zip-src/");
```

```text
1290 files, 9,933,274 bytes
      1,466  Asm/arm/7zCrcOpt.asm
      3,146  Asm/arm64/7zAsm.S
     37,757  Asm/arm64/LzmaDecOpt.S
      5,785  Asm/x86/7zAsm.asm
      6,711  Asm/x86/7zCrcOpt.asm
extracted 1290 files to 7zip-src/
```

`Archive.Open` recognizes the format from the bytes, not the file name, so the same code extracts zip, tar, tar.gz, tar.xz, cpio, ar, ISO and cab files. Extraction is safe by default: nothing is written outside the target folder, and limits on size, compression ratio and entry count are on ([Extract untrusted archives safely](https://recursiveai.org/onearchive/cookbook/safe-extraction/)).

## A password-protected 7z

For a 7z encrypted with AES-256, pass the password when you open it:

```csharp
using RecursiveAI.OneArchive;

using var archive = Archive.Open("secret.7z",
    new ArchiveOpenOptions { Password = "correct horse" });
archive.Extract("secret");
```

A missing or wrong password throws `ArchivePasswordException`, whose `Problem` says `Required` or `Incorrect`.

## Limits

- **No BCJ2 filter.** 7-Zip applies BCJ2 to executables at its higher settings, and such archives (7-Zip’s own `7z2409-extra.7z` is one) throw `ArchiveUnsupportedException`: “the coder with method ID 0x303011B (BCJ2) is not supported”. LZMA, LZMA2, PPMd, bzip2, Deflate and the BCJ, ARM and other single-stream filters are read.
- **RAR is read-only and partial.** RAR 5 archives can be listed, but only entries stored without compression can be extracted.
- **Random access into a solid 7z is slow.** Solid archives compress files together, so reading one entry with `OpenEntry` decodes the entries before it in its block; `Extract`, which reads in order, doesn’t pay that.
- **.NET 10.** The packages target `net10.0`.

To make an archive, see [Create a zip file](https://recursiveai.org/onearchive/cookbook/create-zip/). Every format and option is in [Archives and safety](https://recursiveai.org/onearchive/docs/archive/).
