The constructor of GZipStream
has an overload with a leaveOpen
parameter.
So when you need to access the MemoryStream after the GZip has Closed (and implicitly, Flushed), pass true
to that.
using (var ms = new MemoryStream())
{
using (var gz = new GZipStream(ms, CompressionMode.Compress, leaveOpen: true))
{
// ... write to gz
}
Console.WriteLine(ms.Length); // this is the final and accurate length
}
This still leaves the GetArray() vs getBuiffer() problem but now you can use the buffer with an accurate length.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…