OneArchive is a free, open-source .NET library from Recursive AI, licensed MIT or Apache-2.0. Its OneCompress package provides all three decoders. NuGet · GitHub.
Use OneCompress when you need the decoder to enforce a byte limit on decompressed output or to read zlib data that requires a preset dictionary.
dotnet add package RecursiveAI.OneCompress --version 1.2.0In a .NET 10 console project, replace Program.cs and run dotnet run.
using System.IO.Compression; using System.Text; using RecursiveAI.OneCompress; using RecursiveAI.OneCompress.Deflate; byte[] input = Encoding.UTF8.GetBytes("hello from .NET"); var limits = new DeflateDecompressionOptions { MaxOutputSize = 1024 }; foreach (string format in new[] { "gzip", "zlib", "raw" }) { using var buffer = new MemoryStream(); using (Stream encoder = format switch { "gzip" => new GZipStream(buffer, CompressionMode.Compress, leaveOpen: true), "zlib" => new ZLibStream(buffer, CompressionMode.Compress, leaveOpen: true), _ => new DeflateStream(buffer, CompressionMode.Compress, leaveOpen: true) }) encoder.Write(input); byte[] received = buffer.ToArray(); byte[] decoded = format switch { "gzip" => GZip.Decompress(received, limits), "zlib" => ZLib.Decompress(received, limits), _ => Deflate.Decompress(received, limits) }; Console.WriteLine($"{format}: {Encoding.UTF8.GetString(decoded)}"); if (format == "raw") continue; received[^1] ^= 1; // Change a trailer byte, not the compressed payload. try { if (format == "gzip") GZip.Decompress(received, limits); else ZLib.Decompress(received, limits); throw new InvalidOperationException("Expected a trailer error."); } catch (CompressionFormatException ex) { Console.WriteLine($"{format} damaged trailer: {ex.GetType().Name}"); } }
gzip: hello from .NET gzip damaged trailer: CompressionFormatException zlib: hello from .NET zlib damaged trailer: CompressionFormatException raw: hello from .NET
Choose the decoder from the format the sender specifies: GZip for gzip, ZLib for zlib, and Deflate only for raw Deflate. In particular, HTTP Content-Encoding: deflate calls for zlib, not raw Deflate.
What each wrapper adds
| Incoming format | Before the Deflate blocks | After the blocks | Reader: OneCompress / .NET |
|---|---|---|---|
| gzip | Starts with 1F 8B 08 for Deflate; flags can add fields such as a filename or a header checksum. | Little-endian CRC-32 of the decoded bytes and ISIZE, the decoded length modulo 232, per member. | GZip.Decompress / GZipStream |
| zlib | CMF and FLG: method, window, flags, and a header check. If FDICT is set, a four-byte dictionary ID follows. | Big-endian Adler-32 of the decoded bytes. | ZLib.Decompress / ZLibStream |
| Raw Deflate | No wrapper; the stream starts with a Deflate block header. | No wrapper checksum or size field. | Deflate.Decompress / DeflateStream |
The program writes one message with the three .NET streams and reads it with OneCompress. Changing the final gzip byte changes ISIZE; changing the final zlib byte changes Adler-32. Neither edit touches the compressed blocks. OneCompress checks both gzip trailer fields and the zlib checksum by default. These checks detect corruption, not authenticity. See the pinned decoder rules.
Choose by protocol first, bytes second
- Use the protocol or container when it is known. RFC 9110 §8.4.1.2 defines HTTP
deflateas zlib; it also notes that some nonconforming implementations send raw Deflate. If you must support a known broken sender, make that an explicit compatibility setting rather than falling back to raw on every checksum failure. For a ZIP file, use a ZIP reader; a raw decoder expects the compressed entry bytes, not the archive. - When the format is unknown, treat header checks as clues. At the start of a stream,
1F 8B 08suggests gzip. For zlib, check that the low nibble ofCMFis 8, the high nibble is at most 7, and(256 × CMF + FLG)is divisible by 31. A matching prefix is not proof; the full stream still needs to decode. Raw Deflate has no fixed magic, so a nonmatching prefix does not establish that it is raw. - Use a dictionary only when you have the sender’s bytes. In zlib, the dictionary ID is its Adler-32, not the dictionary itself. Supply the agreed bytes via
DeflateDecompressionOptions.Dictionary; OneCompress reports a missing or mismatched dictionary asCompressionDictionaryException.
Set a size limit and check the end of the input
The example uses MaxOutputSize = 1024 so a small message cannot expand past 1 KiB. Use your protocol’s actual limit rather than treating gzip’s ISIZE as a trustworthy allocation size. For incremental reads, OneCompress offers DeflateCodecStream and DeflateDecoder with an explicit DeflateFormat.
The one-shot gzip reader accepts consecutive gzip members. The raw and zlib readers reject bytes after a complete stream by default. If you use DeflateDecoder directly, Done ends one gzip member, not necessarily the whole input: pass the remaining bytes to read the next member. See the unit boundaries and limits and errors.
For file examples, see gzip interoperability and zlib compression and decompression.