Riddle me this, why won’t this code work?
time to read 2 min | 305 words
The following code will not result in the expected output:
using(var mem = new MemoryStream()) { using(var gzip = new GZipStream(mem, CompressionMode.Compress, leaveOpen:true)) { gzip.WriteByte(1); gzip.WriteByte(2); gzip.WriteByte(1); gzip.Flush(); } using (var gzip = new GZipStream(mem, CompressionMode.Compress, leaveOpen: true)) { gzip.WriteByte(2); gzip.WriteByte(1); gzip.WriteByte(2); gzip.Flush(); } mem.Position = 0; using (var gzip = new GZipStream(mem, CompressionMode.Decompress, leaveOpen: true)) { Console.WriteLine(gzip.ReadByte()); Console.WriteLine(gzip.ReadByte()); Console.WriteLine(gzip.ReadByte()); } using (var gzip = new GZipStream(mem, CompressionMode.Decompress, leaveOpen: true)) { Console.WriteLine(gzip.ReadByte()); Console.WriteLine(gzip.ReadByte()); Console.WriteLine(gzip.ReadByte()); } }
Why? And what can be done to solve this?

Comments
Comment preview