[
  {
    "Id": "239667",
    "ThreadId": "70364",
    "Html": "<p>Hi,</p>\r\n<p>I am attempting to compress and decompress some byte arrays using version 1.9 of the libraries.&nbsp; Everything goes as expected, but sometimes I get a ZlibException &quot;inflating:&quot; from the library.&nbsp; Unfortunately, it isn't every time, just sometimes.&nbsp; This occurs when I am attempting to Close() or Dispose() of the DeflateStream after everything has worked out correctly.&nbsp; My code is as follows:</p>\r\n<p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>                <span style=\"color:Blue\">int</span> compressedLength;\r\n                <span style=\"color:Blue\">byte</span>[] bufferedBytes;\r\n\r\n                MemoryStream memoryStream = <span style=\"color:Blue\">new</span> MemoryStream();\r\n                DeflateStream deflateStream = <span style=\"color:Blue\">new</span> DeflateStream(memoryStream, CompressionMode.Decompress, <span style=\"color:Blue\">false</span>);\r\n\r\n                bufferedBytes = <span style=\"color:Blue\">new</span> <span style=\"color:Blue\">byte</span>[buffer.Count];\r\n                Array.Copy(buffer.Array, buffer.Offset, bufferedBytes, 0, buffer.Count);\r\n                deflateStream.Write(bufferedBytes, 0, bufferedBytes.Length);\r\n\r\n                compressedLength = (<span style=\"color:Blue\">int</span>)memoryStream.Length;\r\n                bufferedBytes = bufferManager.TakeBuffer(compressedLength + buffer.Offset);\r\n                Array.Copy(buffer.Array, 0, bufferedBytes, 0, buffer.Offset);\r\n                Array.Copy(memoryStream.ToArray(), 0, bufferedBytes, buffer.Offset, compressedLength);\r\n\r\n                memoryStream.Flush();\r\n                memoryStream.Close();\r\n                memoryStream.Dispose();\r\n\r\n                deflateStream.Flush();\r\n                deflateStream.Close();\r\n                deflateStream.Dispose();\r\n</pre>\r\n</div>\r\n</p>\r\n<p>The exception detail is as follows:</p>\r\n<p style=\"padding-left:120px\">Ionic.Zlib.ZlibException was unhandled by user code<br>&nbsp; Message=&quot;inflating: &quot;<br>&nbsp; Source=&quot;Ionic.Zlib&quot;<br>&nbsp; StackTrace:<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at Ionic.Zlib.ZlibBaseStream.finish()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at Ionic.Zlib.ZlibBaseStream.Close()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at Ionic.Zlib.DeflateStream.Dispose(Boolean disposing)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.IO.Stream.Close()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at PCAPPS.DataServices.WCF.DataService.DeflateMessageEncoderFactory.DeflateMessageEncoder.DecompressBuffer_Ionic(ArraySegment`1 buffer, BufferManager bufferManager) in C:\\Documents and Settings\\dvajda\\My Documents\\Visual Studio 2008\\Projects\\WCF\\WCF.DataContracts\\DeflateMessageEncoderFactory.cs:line 230<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at PCAPPS.DataServices.WCF.DataService.DeflateMessageEncoderFactory.DeflateMessageEncoder.ReadMessage(ArraySegment`1 buffer, BufferManager bufferManager, String contentType) in C:\\Documents and Settings\\dvajda\\My Documents\\Visual Studio 2008\\Projects\\WCF\\WCF.DataContracts\\DeflateMessageEncoderFactory.cs:line 52<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceModel.Channels.HttpInput.DecodeBufferedMessage(ArraySegment`1 buffer, Stream inputStream)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceModel.Channels.HttpInput.ReadBufferedMessage(Stream inputStream)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceModel.Channels.HttpInput.ParseIncomingMessage(Exception&amp; requestException)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceModel.Channels.HttpRequestContext.CreateMessage()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; at System.ServiceModel.Channels.HttpChannelListener.HttpContextReceived(HttpRequestContext context, ItemDequeuedCallback callback)<br>&nbsp; InnerException:</p>\r\n<p>I do not get this exception every time, just SOMETIMES, which is really annoying.&nbsp; But it consistently happens if I run the code in a loop, or if I just keep trying it.&nbsp; I have found a workaround which basically uses DeflateStream.Read() instead of DeflateStream.Write(), but I would really like to use the DeflateStream.Write() method (also, the Read() seems to have a bug in it as well, but I have another posting on this forum about that).&nbsp; My workaround code is as follows:</p>\r\n<p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>                <span style=\"color:Blue\">int</span> compressedLength;\r\n                <span style=\"color:Blue\">byte</span>[] bufferedBytes;\r\n\r\n                MemoryStream memoryStream = <span style=\"color:Blue\">new</span> MemoryStream(buffer.Array, buffer.Offset, buffer.Count);\r\n                DeflateStream deflateStream = <span style=\"color:Blue\">new</span> DeflateStream(memoryStream, CompressionMode.Decompress, <span style=\"color:Blue\">true</span>);\r\n                MemoryStream uncompressedStream = <span style=\"color:Blue\">new</span> MemoryStream();\r\n                \r\n                <span style=\"color:Blue\">int</span> readBytes = 0;\r\n                bufferedBytes = <span style=\"color:Blue\">new</span> <span style=\"color:Blue\">byte</span>[Math.Min(1024, buffer.Count)];\r\n                <span style=\"color:Blue\">while</span> ((readBytes = deflateStream.Read(bufferedBytes, 0, bufferedBytes.Length)) &gt; 0)\r\n                    uncompressedStream.Write(bufferedBytes, 0, readBytes);\r\n</pre>\r\n</div>\r\n</p>\r\n<p>Any input as to how I can avoid the &quot;inflating:&quot; exception would be much appreciated, as I would really like to use the first code format (involving Write()) for efficiency in my code.</p>",
    "PostedDate": "2009-09-28T12:12:52.717-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "239678",
    "ThreadId": "70364",
    "Html": "<p>I delved into the source code for the ZlibBaseStream, and I think I found the issue.&nbsp; In my code, I am attempting to decompress, but the error says &quot;inflating: &quot;...&nbsp; Looking at the internal private members and variables of my DeflateStream during the exception, I can see that the base stream of my DeflateStream has _wantCompress set to false, which is correct.&nbsp; But the source code for ZlibBaseStream in the finish() function looks as follows:</p>\r\n<p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>                    <span style=\"color:Blue\">int</span> rc = (_wantCompress)\r\n                        ? _z.Deflate(FlushType.Finish)\r\n                        : _z.Inflate(FlushType.Finish);\r\n\r\n                    <span style=\"color:Blue\">if</span> (rc != ZlibConstants.Z_STREAM_END &amp;&amp; rc != ZlibConstants.Z_OK)\r\n                        <span style=\"color:Blue\">throw</span> <span style=\"color:Blue\">new</span> ZlibException((_wantCompress ? <span style=\"color:#A31515\">&quot;de&quot;</span> : <span style=\"color:#A31515\">&quot;in&quot;</span>) + <span style=\"color:#A31515\">&quot;flating: &quot;</span> + _z.Message);\r\n</pre>\r\n</div>\r\n</p>\r\n<p>This seems incorrect to me.&nbsp; If _wantCompress is false, as it should be, then it seems like the shorthand if statements are backwards, ie, I would want to call _z.Deflate(FlushType.Finish) and the error should at least say &quot;deflating: &quot; not &quot;inflating: &quot;.&nbsp; Any thoughts, it just seems like the two if statements are backwards.&nbsp; I think the first one causes rc to be incorrectly set, which then causes exception to be thrown.</p>\r\n<p>Or I'm just way off.</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-09-28T12:36:19.573-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "239877",
    "ThreadId": "70364",
    "Html": "This discussion has been copied to a work item. Click <a href=\"http://dotnetzip.codeplex.com/WorkItem/View.aspx?WorkItemId=8870\">here</a> to go to the work item and continue the discussion.",
    "PostedDate": "2009-09-29T00:41:07.48-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "239945",
    "ThreadId": "70364",
    "Html": "<p>This is a bug in DotNetZip.</p>\r\n<p>It's been fixed in changeset 44267.&nbsp; First binary with the fix is 1.9.0.11, will be available soon.</p>\r\n<p>I don't know what you are trying to say with your second mail.&nbsp; Your application used the DeflateStream in Decompress mode, which means Inflate.&nbsp; The problem you had was during inflation.&nbsp; The logic in that code you cited is correct.</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>",
    "PostedDate": "2009-09-29T05:27:33.89-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]