Search code examples
c#gzipstream

Why the length of zipped stream is always 10?


I am testing some code. I am stuck with the following. What ever I write as text, the length of the zipped stream is always 10? What am I doing wrong?

var inStream = new MemoryStream();

var inWriter = new StreamWriter(inStream);

str text = "HelloWorldsasdfghj123455667880fgsjfhdfasdferrbvbyjun hbwecwcxqsz    edcrgvebrjnuj5juerqwetsrgfggshurhtnbvzkfjhguhgrgal;kjhao;rhl;zkfhg;aorihghg;oahrgarhguhh';aaeaeiaijeihjrhfidfhfidfidhh953453453";
inWriter.WriteLine(text);

inWriter.Flush();
inStream.Position = 0;

var outStream = new MemoryStream();
var compressStream = new GZipStream(outStream, CompressionMode.Compress);
inStream.CopyTo(compressStream);

compressStream.Flush();
outStream.Flush();
compressStream.Flush();

outStream.Position = 0;

Console.WriteLine(outStream.Position);
Console.WriteLine(outStream.Length);

Solution

  • Until you Close it the compression stream doesn't know you've finished writing to it - so cannot complete its compression algorithm. Flushing flushes those parts it can flush, but until its been told you have completed adding new bytes it cannot flush its last package of compressed data.