"Block length does not match with its complement." in DeflateStream

Published on Sunday, September 9, 2007
I wanted to discuss a problem that I faced while implementing the repository layer of DotSVN.
When I tried to use the System.IO.Compression.DeflateStream to decompress the repository, I get the exception 'System.IO.InvalidDataException' : "Block length does not match with its complement."

I Googled around an found this article that explains the problem. The feedback from Microsoft on how to fix this issue is as follows:
...skipping past the first two bytes solved the problem. Those bytes are part of the zlib specification (RFC 1950), not the deflate specification (RFC 1951). Those bytes contain information about the compression method and flags.

The zlib and deflate formats are related; the compressed data part of zlib-formatted data may be stored in the deflate format. In particular, if the compression method in the zlib header is set to 8, then the compressed data is stored in the deflate format. This is true in the case of the stream you submitted, which was taken from a pdf file.

Our DeflateStream, on the other hand, represents the deflate specification (RFC 1951) but not RFC 1950. So your workaround of skipping past the first 2 bytes to get to the deflate data will definitely work.
So the actual code would look like the following:

// create a file stream, as before
FileStream fs = new FileStream("file", FileMode.Open);

// skip first two bytes
fs.ReadByte();
fs.ReadByte();

// now create the deflate stream and proceed as usual
DeflateStream l_Stream = new DeflateStream(fs, CompressionMode.Decompress);
comments powered by Disqus