- December
Posted By : Alexander Goida
Compressing of data with GZipStream

GZipStream is a great tool to compress/decompress byte arrays of serialized data. But it has an issue which can make you hanging for some time. Here I describe the issue with mismatch of compressed and decompressed data and provide a solution.
Compressing data could be useful when you need to transfer a data across distributed system (e.g. between web service and its client applications). The first time when I need in that tool was when I was writing the tool which generates a huge sample of statistical data which should be stored in a file and transferred via . Thanks to Microsoft guys, they have already thought about that and created the useful class GZipStream (find more in MSDN). I didn’t find any alternative for that tool so I’d rather say this is most proper tool for to extend your application with compression/decompression ability.

Data Mismatch

While I was implementing my code I have been stacked for some time on unknown behavior for that time of the class. After decompressing a new instance of byte array didn’t match to its original version. The last byte was zero when its original version had a non-zero value. As I figured out the reason if that GZipStream has internal cache which must be closed before using of a compressed array.

The sample below shows the issue and what caused it. The incorrect version of the code sample:

At the time when I call ms.ToArray();  ZIP stream is not finished its work and unable to store all data to the memory stream. This the reason of my issue which can be solved by closing ZIP stream before storing byte array. I rewrote the code in this way:

Code Snippet

The class ZipData can be used to transfer compressed data between tiers.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.