ZLib Exception when reading large chunks

I've been trying to figure out for hours why the Ionic.Zlib.ZlibStream wasn't decompressing data properly and instead throwing an exception with "Bad state (incorrect data check)"

In all my tests it worked but when using the stream in a real environment it would blow up in my face. As it turns out, in my tests I read the compressed stream in chunks of 4096 bytes which is fine, and in a real situation it was requesting chunks of 256K.

After a bit of fiddling it seems that trying to read out of a compressed stream in chunks any larger than 5095 bytes results in the above error. So for example:

There's a problem with the Adler32 checksum calculator which means it calculates the wrong value if a single block of data causes an internal int32 to overflow. The number of bytes required to cause this depends on the content of the data - it's
3980 bytes of 0xFF's, or larger chunks if the data contains smaller values (as is probably the case in your example). To be on the safe side with the current version you should use a buffer <3980, or you can create a private build which contains
a fix (attached to the link above) which resolves the problem.

Note also that if this bug is triggered during compression DotNetZip will stamp the zip with an incorrect Adler32 checksum. You might not notice this if you only ever try to decompress it with the same version of DotNetZip since it will calculate the same
incorrect (but matching) checksum. However, other zip clients will complain in various ways and may refuse to unpack it since they'll think it's become corrupted.