The compression code does not work like encryption - you cannot decompress from one stream to another, writing compressed data. You must provide a stream that contains the compressed data, and let GZipStream read from it. Something like that:
using (Stream file = File.OpenRead(filename)) using (Stream gzip = new GZipStream(file, CompressionMode.Decompress)) using (Stream memoryStream = new MemoryStream()) { CopyStream(gzip, memoryStream); return memoryStream.ToArray(); }
CopyStream is a simple utility method for reading from one stream and copying all data to another. Something like that:
static void CopyStream(Stream input, Stream output) { byte[] buffer = new byte[8192]; int bytesRead; while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) { output.Write(buffer, 0, bytesRead); } }
Jon skeet
source share