zlib wraps Deflate with a header and checksum.
dotnet add package RecursiveAI.OneCompressRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneCompress.Deflate; byte[] data = "hello zlib"u8.ToArray(); byte[] packed = ZLib.Compress(data); byte[] unpacked = ZLib.Decompress(packed); Console.WriteLine(unpacked.Length);
10Unzip the example, 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.
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:
hello zlib Round trip: True zlib bytes: 789CCB48CDC9C957A8CAC94C0200159503E6
Variations
Choose a compression level. This standalone snippet uses level 9 instead of the default 6:
using RecursiveAI.OneCompress.Deflate; byte[] packed = ZLib.Compress("hello zlib"u8, new DeflateOptions { Level = 9 }); Console.WriteLine(Convert.ToHexString(packed));
78DACB48CDC9C957A8CAC94C0200159503E6Related recipes
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 describes the supported wrappers, checks and options.
What is RFC 1950?
RFC 1950 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; gzip has its own wrapper in RFC 1952. 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. 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.