---
title: "Compress and decompress zlib in C#"
url: https://recursiveai.org/onearchive/cookbook/compress-decompress-zlib/
description: "zlib wraps Deflate-compressed bytes with a header and an Adler-32 checksum. Runnable C# example using OneCompress."
version: "1.2.0"
---

# Compress and decompress zlib in C#

**OneArchive** is a free, open-source archive and compression library for .NET from Recursive AI: archives (zip, tar, 7z, RAR 5, cpio, ar, ISO) in RecursiveAI.OneArchive, codecs (zlib, gzip, xz, Zstandard, Brotli) in RecursiveAI.OneCompress. MIT or Apache-2.0, no native dependencies.

[NuGet](https://www.nuget.org/packages/RecursiveAI.OneCompress) · [GitHub](https://github.com/recursive-ai-oss/onearchive-csharp) · [Docs](https://recursiveai.org/onearchive/docs/) · [Download example (.zip)](https://recursiveai.org/onearchive/cookbook/compress-decompress-zlib/compress-decompress-zlib.zip)

zlib wraps Deflate with a header and checksum.

```shell
dotnet add package RecursiveAI.OneCompress
```

Requires [.NET 10](https://dotnet.microsoft.com/download/dotnet/10.0) (net10.0); .NET 9 and earlier aren’t supported.

```csharp
using RecursiveAI.OneCompress.Deflate;
byte[] data = "hello zlib"u8.ToArray();
byte[] packed = ZLib.Compress(data);
byte[] unpacked = ZLib.Decompress(packed);
Console.WriteLine(unpacked.Length);
```

```text
10
```

Unzip [the example](https://recursiveai.org/onearchive/cookbook/compress-decompress-zlib/compress-decompress-zlib.zip), then `dotnet run`.

## The full program

The input is the UTF-8 text `hello zlib`, created in memory; no sample file is needed. The program prints the restored text, compares the bytes and shows the complete zlib stream in hexadecimal.

Corrupt or truncated input throws `CompressionFormatException`; output above `MaxOutputSize` throws `CompressionLimitException`.

```csharp
using System.Text;
using RecursiveAI.OneCompress.Deflate;

byte[] input = Encoding.UTF8.GetBytes("hello zlib");
byte[] compressed = ZLib.Compress(input);
byte[] restored = ZLib.Decompress(compressed);
Console.WriteLine(Encoding.UTF8.GetString(restored));
Console.WriteLine($"Round trip: {input.AsSpan().SequenceEqual(restored)}");
Console.WriteLine($"zlib bytes: {Convert.ToHexString(compressed)}");
```

`dotnet run` prints:

```text
hello zlib
Round trip: True
zlib bytes: 789CCB48CDC9C957A8CAC94C0200159503E6
```

## Variations

**Choose a compression level.** This standalone snippet uses level 9 instead of the default 6:

```csharp
using RecursiveAI.OneCompress.Deflate;

byte[] packed = ZLib.Compress("hello zlib"u8,
    new DeflateOptions { Level = 9 });
Console.WriteLine(Convert.ToHexString(packed));
```

```text
78DACB48CDC9C957A8CAC94C0200159503E6
```

## Related recipes

- [Read and write gzip in C#](https://recursiveai.org/onearchive/cookbook/gzip-interop/)
- [Compress and decompress LZ4](https://recursiveai.org/onearchive/cookbook/lz4-compress-decompress/)
- [Decompress an xz file](https://recursiveai.org/onearchive/cookbook/decompress-xz-file/)

<!-- article -->

## The short answer

Use `ZLib.Compress` to turn a byte buffer into a zlib stream and `ZLib.Decompress` to get the bytes back. Both methods are in `RecursiveAI.OneCompress.Deflate`, supplied by the `RecursiveAI.OneCompress` package. You don’t need the separate archive package for this task.

The snippet at the top compresses and restores UTF-8 bytes and prints their recovered length, `10`. The full program also prints the compressed bytes so you can inspect the wrapper rather than mistaking it for raw Deflate. The [gzip and zlib documentation](https://recursiveai.org/onearchive/docs/gzip-zlib/) describes the supported wrappers, checks and options.

## What is RFC 1950?

[RFC 1950](https://www.rfc-editor.org/rfc/rfc1950.html#section-2.2) specifies the zlib compressed-data format. Its stream begins with a header describing the compression method and flags. A preset dictionary, when used, adds a dictionary identifier. The compressed data follows, with an Adler-32 checksum of the uncompressed data at the end.

For this example, `78 9C` is the header and `15 95 03 E6` is the checksum. The middle bytes carry the Deflate data. These bytes come from the program’s output, not from a general rule that every zlib stream must start with that exact pair. The level-9 variation starts with `78 DA` instead.

Raw Deflate is specified separately in [RFC 1951](https://www.rfc-editor.org/rfc/rfc1951.html); gzip has its own wrapper in [RFC 1952](https://www.rfc-editor.org/rfc/rfc1952.html). Pick the decoder for the format your producer actually writes.

## How do I decompress zlib data in C#?

Pass the complete zlib stream to `ZLib.Decompress`. Keep its header and checksum. The method returns the uncompressed bytes; it does not assume they are text. Only call `Encoding.UTF8.GetString` when the original content is UTF-8. For images or other binary content, keep the returned byte array.

The decoder verifies the checksum by default. Its default output limit is 1 GiB, as documented in the [codec guide](https://recursiveai.org/onearchive/docs/gzip-zlib/). For an application with a smaller expected response, set `DeflateDecompressionOptions.MaxOutputSize` to that application’s byte limit. A limit failure is an exception, not a successful partial result. A stream requiring a preset dictionary also needs the matching dictionary; missing or mismatched dictionaries raise `CompressionDictionaryException`.

## How do I compress a string in C#?

Choose an encoding first. The example uses UTF-8, compresses those bytes, then decodes the restored bytes with the same encoding. Compression is a binary operation: the result is not a printable string. If a text-only transport needs the result, encode the compressed bytes as Base64 separately; Base64 is not compression.

`DeflateOptions.Level` controls compression. OneCompress defaults to 6 and supports 0 through 12: 0 stores the data, 1 through 9 use the zlib scale, and 10 through 12 use optimal parsing. The level-9 example shows how to choose a level without changing the decompression call. A reader does not need to be told which level produced the stream.
