---
title: "Create a zip file in C#"
url: https://recursiveai.org/onearchive/cookbook/create-zip/
description: "Create a zip file in C#: zip a folder and single files into an archive that every zip tool opens, then list it. A complete program with OneArchive."
version: "1.0.0"
---

# Create a zip file in C#

Call `Archive.Create` with a `.zip` path, add folders and files, and `Save`. The program below makes a small report folder, zips it with a README stored under another name, and lists the result.

```shell
dotnet new console -o CreateZip && cd CreateZip
dotnet add package RecursiveAI.OneArchive --version 1.0.0
```

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

```csharp
using RecursiveAI.OneArchive;

// Something to zip: a folder with two files, and a README.
Directory.CreateDirectory("report");
File.WriteAllText("report/summary.txt", "Q3: 444 orders shipped.\n");
File.WriteAllLines("report/orders.csv",
    Enumerable.Range(1, 500).Select(i => $"{i},order-{i},shipped"));
File.WriteAllText("README.md", "# Q3 report\n");

// The format comes from the extension: .zip here.
Archive.Create("q3.zip")
    .Add("report/")                       // a folder, with everything in it
    .Add("README.md", "docs/readme.md")   // a file, stored under another name
    .Save();

Console.WriteLine($"q3.zip: {new FileInfo("q3.zip").Length:N0} bytes");
using var zip = Archive.Open("q3.zip");
foreach (var e in zip.Entries)
    Console.WriteLine($"  {e.Kind,-9} {e.Size,7:N0}  {e.Name}");
```

```text
q3.zip: 2,489 bytes
  Directory       0  report
  File       10,784  report/orders.csv
  File           24  report/summary.txt
  File           12  docs/readme.md
```

The zip is a standard one, compressed with Deflate: Windows Explorer, macOS, `unzip` and .NET’s own `ZipFile` open it. To unzip it again, `Archive.Extract("q3.zip", "out")` writes the files into `out/`, and refuses paths that would escape it.

## Limits

- **Deflate by default.** `ZipWriterOptions.Method` can choose bzip2, Deflate64 or Zstandard instead, but fewer zip tools open those.
- **No password here.** For one, see [Create a password-protected zip](https://recursiveai.org/onearchive/cookbook/zip-encryption/), which uses WinZip AES-256: 7-Zip and WinZip open it, and not every built-in unzip tool does.
- **.NET 10.** The packages target `net10.0`.

The same call writes `.tar.gz`, `.tar.zst` and `.tar.xz` archives: change the extension. Every option is in [Archives and safety](https://recursiveai.org/onearchive/docs/archive/).
