ZLib decompression

I am trying to compress data using zlib.net library. Regardless of the contents of the uncompressed string, it seems to me that in raw [] there are only two bytes of data.

{ string uncompressed = "1234567890"; byte[] data = UTF8Encoding.Default.GetBytes(uncompressed); MemoryStream input = new MemoryStream(data); MemoryStream output = new MemoryStream(); Stream outZStream = new ZOutputStream(output,zlibConst.Z_DEFAULT_COMPRESSION); CopyStream(input, outZStream); output.Seek(0, SeekOrigin.Begin); byte[] raw = output.ToArray(); string compressed = Convert.ToBase64String(raw); } public void CopyStream(System.IO.Stream input, System.IO.Stream output) { byte[] buffer = new byte[2000]; int len; while ((len = input.Read(buffer, 0, 2000)) > 0) { output.Write(buffer, 0, len); } output.Flush(); } 
+1
c # zlib decompression
source share
1 answer

The problem is that ZOutputStream actually writes some information to the stream in the finish () method (which is called Close). The Close method also closes the underlying thread, so this is not very useful in this situation.

Changing the code to the following should work:

 { string uncompressed = "1234567890"; byte[] data = UTF8Encoding.Default.GetBytes(uncompressed); MemoryStream input = new MemoryStream(data); MemoryStream output = new MemoryStream(); ZOutputStream outZStream = new ZOutputStream(output,zlibConst.Z_DEFAULT_COMPRESSION); CopyStream(input, outZStream); outZStream.finish(); output.Seek(0, SeekOrigin.Begin); byte[] raw = output.ToArray(); string compressed = Convert.ToBase64String(raw); } public void CopyStream(System.IO.Stream input, System.IO.Stream output) { byte[] buffer = new byte[2000]; int len; while ((len = input.Read(buffer, 0, 2000)) > 0) { output.Write(buffer, 0, len); } output.Flush(); } 
+3
source share

All Articles