Friday, March 02, 2007

DeflateStream and zlib headers

The DeflateStream class in System.IO.Compression, as the docs state, represents the DEFLATE algorithm as in RFC 1951.

What it doesn't do (out of the box) is support compressed data in zlib format (see RFC 1950). When trying to decompress existing data, the easiest way I've found to get round this is simply to get rid of the 2 byte zlib header and use what's left, so something like the following works nicely if you've already got the data as a byte array in memory:


Array.Copy(zlibBytes, 2, strippedBytes, 0, zlibBytes.Length - 2);


Then just use the strippedBytes with DeflateStream instead.

Out of interest, I also tried stripping off the 4 byte Adler-32 checksum footer, and DeflateStream worked happily with that too.

No comments: