I have the following piece of code:
MemoryStream resultStream = new MemoryStream();
string users = ""//Really long string goes here
BinaryFormatter bFormatter = new BinaryFormatter();
using (MemoryStream assignedUsersStream = new MemoryStream())
{
bFormatter.Serialize(assignedUsersStream, users);
assignedUsersStream.Position = 0;
using (var compressionStream =
new DeflateStream(resultStream, CompressionLevel.Optimal))
{
assignedUsersStream.CopyTo(compressionStream);
Console.WriteLine("Compressed from {0} to {1} bytes.",
assignedUsersStream.Length.ToString(),
resultStream.Length.ToString());
}
}
the thing is that resultStream
is always empty!
What am I doing wrong here?
From the original answer (I don't have enough credits to vote down)
This is incomplete and misleading. DeflateStream closes the underlying resultStream once DeflateStream goes out of scope. Therefore resultStream.Length throws
Thomas Levesque is correct => also set
leaveOpen
to true.An interesting question with some good points raised by HH and TL.
That is because the
DeflateStream
doesn't flush the data to the underlying stream until it is closed. After it is closed,resultStream
will contain the compressed data. Note that by default,DeflateStream
closes the underlying stream when it's closed, but you don't want that, so you need to passtrue
for theleaveOpen
parameter. Also, you don't need 2 memory streams, you can just serialize directly to thecompressionStream
:Put your verification WriteLine outside of the using. The buffers haven't been flushed yet.
And aside, you don't need all those
ToString()
s in a writeline.PS: All a BinaryFormatter does with a string is write the bytes with length prefix. If you don't need the prefix (my guess), it could become:
The reverse is just as easy but you'll need an estimate of the maximum length to create the read-buffer: