[
  {
    "Id": "187748",
    "ThreadId": "55596",
    "Html": "<div>        internal static int Deflate(byte[] Input, ref byte[] Output)</div><div>        {</div><div>            int totalOut;</div><div>            using (MemoryStream writer = new MemoryStream())</div><div>            {</div><div>                using (ZlibStream compress = new ZlibStream(writer, CompressionMode.Compress, CompressionLevel.LEVEL9_BEST_COMPRESSION))</div><div>                {</div><div>                    compress.Write(Input, 0, Input.Length);</div><div>                    compress.Flush();</div><div>                    //Output = new byte[writer.Length];</div><div>                    totalOut = Convert.ToInt32(writer.Length);</div><div>                    writer.Seek(0, SeekOrigin.Begin);</div><div>                    writer.Read(Output, 0, totalOut);</div><div>                }</div><div>                writer.Close();</div><div>            }</div><div>            return totalOut;</div><div>        }</div><div><br></div><div><br></div><div>first, please download the sample file from http://files.cnblogs.com/unruledboy/buffer.zip</div><div><br></div><div>extract it, then run the following test code:</div><div><br></div><div><div>            byte[] output = new byte[0x8000];</div><div>            int result = Deflate(File.ReadAllBytes(@&quot;buffer.bin&quot;), ref output);</div><div><br></div><div>you will see the result is 2, which means only 2 bytes are returned.</div><div><br></div></div>",
    "PostedDate": "2009-05-07T08:25:48.197-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "187867",
    "ThreadId": "55596",
    "Html": "<p>UnruledBoy, you are doing something not quite right. <br>You are reading from the compressed stream (writer) before the compressor has been closed.<br><br>When using compressing streams, in DotNetZip and in other compression libraries, the usage pattern generally states that you must Close() the compressing stream before attempting to read the compressed bytes.&nbsp; This is because the compressing stream often will buffer some data, and the Close() will implicitly flush all the bytes and append a footer where appropriate (where the compression format calls for a footer).&nbsp; Flush() will not quite suffice in this case. <br><br>By reading the stream before it is closed, you are getting an incomplete output.&nbsp; In the case of the ZlibStream, I believe the buffer size is 32k, and you have read only 2 bytes.&nbsp; I suspect these two bytes are header bytes, and the actual compressed data has not yet been written to the MemoryStream (writer), at the time you read. <br><br>One other minor thing - you have typed the output to be ref, which is not necessary in your usage. <br><br>Here's some updated code that should work for you:</p>\r\n<pre>      <span style=\"color:#a020f0\">internal static int Deflate(byte[] input, byte[] output)</span>\r\n      {\r\n          <span style=\"color:#228b22\">int</span> <span style=\"color:#b8860b\">totalOut</span>;\r\n          <span style=\"color:#a020f0\">using</span> (<span style=\"color:#228b22\">MemoryStream</span> <span style=\"color:#b8860b\">writer</span> = <span style=\"color:#a020f0\">new</span> MemoryStream())\r\n          {\r\n              <span style=\"color:#a020f0\">using</span> (<span style=\"color:#228b22\">ZlibStream</span> <span style=\"color:#b8860b\">compress</span> = <span style=\"color:#a020f0\">new</span> ZlibStream(writer, <span style=\"color:#5f9ea0\">CompressionMode</span>.Compress, <span style=\"color:#5f9ea0\">CompressionLevel</span>.LEVEL9_BEST_COMPRESSION, <span style=\"color:#5f9ea0\">true</span>))\r\n              {\r\n                  <span style=\"color:#5f9ea0\">compress</span>.Write(input, 0, <span style=\"color:#5f9ea0\">input</span>.Length);\r\n              }\r\n              totalOut = <span style=\"color:#5f9ea0\">Convert</span>.ToInt32(<span style=\"color:#5f9ea0\">writer</span>.Length);\r\n              <span style=\"color:#5f9ea0\">writer</span>.Seek(0, <span style=\"color:#5f9ea0\">SeekOrigin</span>.Begin);\r\n              <span style=\"color:#5f9ea0\">writer</span>.Read(output, 0, totalOut);\r\n          }\r\n          <span style=\"color:#a020f0\">return</span> totalOut;\r\n      }\r\n</pre>\r\n<p>When I run this code I get a compressed size of 20216 bytes&nbsp;when compressing your buffer.bin file.&nbsp;<br><br>The main change is that I have moved the call to writer.Read() to outside the using() clause for the compressor. (Using() implicitly calls Close/Dispose upon exit from the using scope). I had to modify the code to employ the ZlibStream constructor that leaves the underlying stream open. <br><br>This is not very robust code - there is the obvious problem that the output may not be large enough for the compressed data. But it illustrates the point.</p>",
    "PostedDate": "2009-05-07T13:24:12.877-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "187952",
    "ThreadId": "55596",
    "Html": "<p>It's strange, I could compress other files, so far only this file. But I got your point.</p>\r\n<p>great thanks for your always nice and extensively detail explanation. now it works as expected. thanks for your nice library, again:)</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-05-07T18:47:34.013-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]