Partager via


Compressing MemoryStreams with GZipStream

There is a silly pattern to follow while Compressing MemoryStreams with GZipStream. It is documented at https://blogs.msdn.com/b/bclteam/archive/2006/05/10/592551.aspx. Make sure to Close the GZipStream (which will also Close the underlying MemoryStream), and then call the ToArray() method of MemoryStream to return the compressed bytes as an array. Then you can write this array to a new instance of a MemoryStream. I recommend writing extension methods on MemoryStream class to compress and decompress.