I am using ZLIB.Net and I just don't understand what should I do to compress a stream which is not FileStream
, but MemoryStream
. By doing:
byte[] buffer = ASCIIEncoding.ASCII.GetBytes("Hello World");
MemoryStream outStream = new MemoryStream();
zlib.ZOutputStream outZStream = new zlib.ZOutputStream(
outStream,
zlib.zlibConst.Z_BEST_COMPRESSION);
outZStream.Write(buffer, 0, buffer.Length);
outZStream.finish();
buffer = outStream.GetBuffer();
Debug.WriteLine(DateTime.Now.ToString() + ":" + buffer.Length);
MemoryStream inStream = new MemoryStream(buffer);
MemoryStream mo = new MemoryStream();
zlib.ZInputStream inZStream = new zlib.ZInputStream(
inStream,
zlib.zlibConst.Z_BEST_COMPRESSION);
int n = 0;
while ((n = inZStream.Read(buffer, 0, buffer.Length)) > 0)
{
mo.Write(buffer, 0, n);
}
string STR = ASCIIEncoding.ASCII.GetString(mo.GetBuffer());
I can't get the string "Hello World"
back.
longbkit, Thank you for that reference. There is code in there:
It works. But what I see is the only diffrence between Compression and Decompression is Compression Type in ZOutput Constructor...
Amazing. For me would be more clear if Compression is called Output while Decompression - Input. or such.. in fact it's Output only.
❋ user "John Smith" broke original code since CopyTo doesn't work and raises exception in his edited text (And approved twice by others...). Reverting back to original code example by OP. Please test edited code next time before editing, thank you.
Change code.
-> inZStream.read
from sourceCode in ZInputStream.cs