[
  {
    "Id": "222402",
    "ThreadId": "65306",
    "Html": "<p>Hi,</p>\r\n<p>I probably miss something.</p>\r\n<p>i want to send compressed data to WS, without using files. i want to&nbsp;create byte array of the zip file, send it to the WS, and at the WS extract the data into string and manipulate it there.</p>\r\n<p>at the Compressing side i have the following code:</p>\r\n<p>Stream st = new MemoryStream();<br>byte[] ba;<br>using (ZipFile zip = new ZipFile())<br>{<br>&nbsp;zip.AddEntry(&quot;data.txt&quot;, &quot;&quot;, bigSB.ToString());<br>&nbsp;zip.Save(st);<br>&nbsp;ba = new byte[st.Length];<br>&nbsp;st.Write(ba, 0, ba.Length);<br>}<br>st.Close();<br>ws.send(ba);</p>\r\n<p>&nbsp;</p>\r\n<p>Stream st = new MemoryStream();<br>using (ZipFile zip = ZipFile.Read(ba))<br>{<br>&nbsp;zip[0].Extract(st);<br>}<br>StreamReader sr = new StreamReader(st);<br>string myData = sr.ReadToEnd();<br>sr.Close();</p>\r\n<p>at the WS i got an exception:</p>\r\n<p>ZipEntry::ReadHeader(): Bad signature (0x00000000) at position&nbsp; 0x00000000</p>\r\n<p>&nbsp;</p>\r\n<p>what did i do wrong?</p>\r\n<p>&nbsp;</p>\r\n<p>thnx</p>",
    "PostedDate": "2009-08-12T02:15:50.087-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "222485",
    "ThreadId": "65306",
    "Html": "<table border=0 width=800px>\r\n<tbody>\r\n<tr>\r\n<td>\r\n<p>Well you have a bunch of moving parts there.&nbsp; You have the zip library, zipping into and out of streams.&nbsp; You also have the web service communications across those two parts.You'll need good</p>\r\n<p>I would have written the sending side like this:</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre><span style=\"color:Blue\">byte</span>[] ba;\r\n<span style=\"color:Blue\">using</span> (<span style=\"color:Blue\">var</span> ms = <span style=\"color:Blue\">new</span> MemoryStream())\r\n{\r\n  <span style=\"color:Blue\">using</span> (ZipFile zip = <span style=\"color:Blue\">new</span> ZipFile())\r\n  {\r\n    zip.AddEntry(<span style=\"color:#A31515\">&quot;data.txt&quot;</span>, <span style=\"color:#A31515\">&quot;&quot;</span>, bigSB.ToString());\r\n    zip.Save(ms);\r\n  }\r\n  ba = ms.ToArray();\r\n}\r\nws.send(ba);\r\n</pre>\r\n</div>\r\n<p>&nbsp;</p>\r\n<p>On the other side I might have written it like this:</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>String x = <span style=\"color:Blue\">null</span>;\r\n<span style=\"color:Blue\">using</span> (<span style=\"color:Blue\">var</span> ms = <span style=\"color:Blue\">new</span> MemoryStream())\r\n{\r\n    <span style=\"color:Blue\">using</span> (ZipFile zip = ZipFile.Read(ba))\r\n    {\r\n      zip[0].Extract(ms);\r\n    }\r\n    StreamReader sr = <span style=\"color:Blue\">new</span> StreamReader(ms, System.Text.Encoding.Default);\r\n    x = sr.ReadToEnd();\r\n}\r\n\r\n\r\n</pre>\r\n</div>\r\n<p>But even if you get the ZIP part exactly right, you may still have problems.&nbsp; You also have to ensure the communication across the wire is successful, and that on the receiving side, the byte array arrives intact.</p>\r\n<p>It seems to me that all you want to do is compress a single string.&nbsp; In this case using the ZipFile class is probably overkill.&nbsp; A ZipFile is a multi-file compressed archive.&nbsp; But you have just one thing to compress, and it is a string, not a file. &nbsp; You might consider the use of the GZipStream class to compress and decompress just a single string.&nbsp;</p>\r\n<p>Looking at this just now, the code is more complicated that I thought it would be.&nbsp; But this should work:</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre><span style=\"color:Blue\">private</span> <span style=\"color:Blue\">byte</span>[] CompressString(String s)\r\n{\r\n    <span style=\"color:Blue\">var</span> encoding = System.Text.Encoding.UTF8;\r\n    <span style=\"color:Blue\">byte</span>[] uncompressed = encoding.GetBytes(s);\r\n\r\n    <span style=\"color:Blue\">using</span> (<span style=\"color:Blue\">var</span> ms = <span style=\"color:Blue\">new</span> MemoryStream())\r\n    {\r\n        <span style=\"color:Blue\">using</span> (Stream compressor = <span style=\"color:Blue\">new</span> GZipStream(ms, CompressionMode.Compress, CompressionLevel.BestCompression))\r\n        {\r\n            compressor.Write(uncompressed, 0, uncompressed.Length);\r\n        }\r\n        <span style=\"color:Blue\">return</span> ms.ToArray();\r\n    }\r\n}\r\n\r\n       \r\n<span style=\"color:Blue\">private</span> String UncompressString(<span style=\"color:Blue\">byte</span>[] a)\r\n{\r\n    <span style=\"color:Blue\">byte</span>[] working = <span style=\"color:Blue\">new</span> <span style=\"color:Blue\">byte</span>[1024];\r\n    <span style=\"color:Blue\">var</span> encoding = System.Text.Encoding.UTF8;\r\n    <span style=\"color:Blue\">using</span> (<span style=\"color:Blue\">var</span> output = <span style=\"color:Blue\">new</span> MemoryStream())\r\n    {\r\n        <span style=\"color:Blue\">using</span> (<span style=\"color:Blue\">var</span> input = <span style=\"color:Blue\">new</span> MemoryStream(a))\r\n        {\r\n            <span style=\"color:Blue\">using</span> (Stream decompressor = <span style=\"color:Blue\">new</span> GZipStream(input, CompressionMode.Decompress))\r\n            {\r\n                <span style=\"color:Blue\">int</span> n;\r\n                <span style=\"color:Blue\">while</span> ((n= decompressor.Read(working, 0, working.Length)) !=0)\r\n                {\r\n                    output.Write(working, 0, n);\r\n                }\r\n            }\r\n            <span style=\"color:Green\">// reset to allow read from start</span>\r\n            output.Seek(0, SeekOrigin.Begin);\r\n            <span style=\"color:Blue\">var</span> sr = <span style=\"color:Blue\">new</span> StreamReader(output, encoding);\r\n            <span style=\"color:Blue\">return</span> sr.ReadToEnd();\r\n        }\r\n    }\r\n}\r\n\r\n\r\n</pre>\r\n</div>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n</td>\r\n</tr>\r\n</tbody>\r\n</table>",
    "PostedDate": "2009-08-12T06:09:34.31-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "222491",
    "ThreadId": "65306",
    "Html": "This discussion has been copied to a work item. Click <a href=\"http://dotnetzip.codeplex.com/WorkItem/View.aspx?WorkItemId=8460\">here</a> to go to the work item and continue the discussion.",
    "PostedDate": "2009-08-12T06:21:52.683-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]