[
  {
    "Id": "637700",
    "ThreadId": "263989",
    "Html": "\r\n<p>Hello,</p>\r\n<p>I would like my application to zip to a network stream but since a NetworkStream does not support Seek opperations will this not work?</p>\r\n<p>I wrote my application to zip to the stream but the file is always corrupt when its complete.</p>\r\n<p>the code Im using is very simple:</p>\r\n<p></p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:blue\">using</span> (ZipFile zip = <span style=\"color:blue\">new</span> ZipFile())\r\n    {\r\n        zip.AddDirectory(&quot;C:\\\\Test\\\\&quot;);\r\n        Socket socket = <span style=\"color:blue\">new</span> Socket(AddressFamily.InterNetwork, \r\n            SocketType.Stream, ProtocolType.Tcp);\r\n        Socket.Connect(ipAddess, port);\r\n        NetworkStream netStream = <span style=\"color:blue\">new</span> NetworkStream(socket);\r\n        zip.Save(netStream);\r\n        netstream.Flush();\r\n        netStream.Close();\r\n    }\r\n           \r\n</pre>\r\n</div>\r\nThanks for any input\r\n<p></p>\r\n",
    "PostedDate": "2011-07-05T12:18:07.913-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "637705",
    "ThreadId": "263989",
    "Html": "<p>I think your NetworkStream should be in a using clause.&nbsp; Also, I think that using clause should be outside the using clause for the ZipFile.&nbsp;</p>\n<div style=\"background-color: white; color: black;\">\n<pre>  Socket socket = <span style=\"color: blue;\">new</span> Socket(AddressFamily.InterNetwork, \r\n                             SocketType.Stream, ProtocolType.Tcp);\r\n  Socket.Connect(ipAddess, port);\r\n  <span style=\"color: blue;\">using</span> (NetworkStream netStream = <span style=\"color: blue;\">new</span> NetworkStream(socket))\r\n  {\r\n      <span style=\"color: blue;\">using</span> (ZipFile zip = <span style=\"color: blue;\">new</span> ZipFile())\r\n      {\r\n          zip.AddDirectory(<span style=\"color: #a31515;\">\"C:\\\\Test\"</span>);\r\n          zip.Save(netStream);\r\n      }\r\n  }\r\n</pre>\n</div>\n<p>The ZipFile class can save to a non-seekable stream.</p>",
    "PostedDate": "2011-07-05T12:32:52.493-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "637719",
    "ThreadId": "263989",
    "Html": "<p>Great, that worked. If you don't mind me asking, why does that make a difference?</p>",
    "PostedDate": "2011-07-05T13:18:25.063-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "637744",
    "ThreadId": "263989",
    "Html": "<p>The ZipFile is IDisposable.&nbsp; When an instance that supports IDisposable is created in a using clause, the .Dispose() is invoked on it, at the closing curly brace. Not quite exactly - there is a try/finally in there, but that's the best way to think about it.&nbsp; Within the ZipFile.Dispose(), it calls .Close() on any stream that it had written to.&nbsp; In your code, that stream was already closed, because you explicitly called .Flush() and .Close() on the NetworkStream.&nbsp; It makes sense to call .Close() on the ZipFile before calling .Close() on the NetworkStream that the zipfile was being saved into.</p>\r\n<p>As for why specifically it did not work the way you did it, I don't know. I don't know exactly what the flow was, or what bytes may have been missing from the NetworkStream.&nbsp; I just know that it makes sense to me, to treat the ZipFile as the \"inner stream\" and the NetworkStream as an \"outer stream\"; this then dictates the order of the using clauses.</p>\r\n<p>&nbsp;&nbsp;</p>",
    "PostedDate": "2011-07-05T14:29:11.987-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]