[
  {
    "Id": "427952",
    "ThreadId": "208465",
    "Html": "<p>Hello,</p>\r\n<p>&nbsp;&nbsp; I am needing to add an XML file to a ZIP archive.&nbsp; The XML file is not stored on any file system, because it exists only as a character string in a database field.&nbsp; I've been able to successfully pull the XML String out of the database and loaded it into a MemoryStream, but whenever I attempt to add a ZipEntry to my archive and use that MemoryStream as the source, my resultant archive contains a blank file.&nbsp; I have tested, and if I write my XML out to the file system first, then perform a ZipFile.AddFile() call from the newly specified file, the archive is created as expected.&nbsp; However, this is not acceptable in our production environment because only ZIP files and &quot;FIN&quot; files will be allowed to be written to disk.&nbsp; Can anybody help explain why the MemoryStream isn't dumping to the archive?</p>\r\n<p>&nbsp;</p>\r\n<p>\r\n<div style=\"color:Black;background-color:White\">\r\n<pre>MemoryStream stream = <span style=\"color:Blue\">null</span>;\r\ncommand.CommandText = sql;\r\n<span style=\"color:Blue\">using</span> (OracleDataReader reader = command.ExecuteReader())\r\n{\r\n   <span style=\"color:Blue\">while</span> (reader.Read())\r\n   {\r\n       payload = reader[<span style=\"color:#A31515\">&quot;Payload&quot;</span>].ToString();\r\n       stream = <span style=\"color:Blue\">new</span> MemoryStream(payload.Length);\r\n       StreamWriter writer = <span style=\"color:Blue\">new</span> StreamWriter(stream);\r\n       writer.Write(payload);\r\n       <span style=\"color:Blue\">using</span> (ZipFile zip = <span style=\"color:Blue\">new</span> ZipFile())\r\n        {\r\n            ZipEntry entry = zip.AddEntry(<span style=\"color:#A31515\">&quot;payload.xml&quot;</span>, stream);\r\n           <span style=\"color:Green\">//zip.AddFile(&quot;d:\\\\projects\\\\system\\\\Dashboard\\\\payload.xml&quot;);</span>\r\n           zip.AddFile(<span style=\"color:#A31515\">&quot;d:\\\\projects\\\\system\\\\Dashboard\\\\foo.txt&quot;</span>);\r\n           zip.Save(<span style=\"color:#A31515\">&quot;d:\\\\projects\\\\system\\\\Dashboard\\\\payload.zip&quot;</span>);\r\n         }\r\n     }\r\n}\r\n</pre>\r\n</div>\r\n</p>",
    "PostedDate": "2010-04-06T10:29:02.45-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "427970",
    "ThreadId": "208465",
    "Html": "<p>The reason your code is failing is that you have not called Seek() on the memory stream.&nbsp; The Position of the MemoryStream after you have written it, is, by definition, at the end of the stream.&nbsp; At this point you pass the stream as input for ZipFile.AddEntry().&nbsp; At the time of ZipFile.Save(), DotNetZip attempts to read the stream.&nbsp; Remember that the Position of the stream points to the end of the stream.&nbsp; Therefore there is nothing to read.&nbsp; If you call Seek(0,SeekOrigin.Begin), before calling ZipFile.AddEntry(), then it will work.</p>\r\n<p>But, you don't need the stream.&nbsp; Why not just use the ZipFile.AddEntry overload that accepts a string?</p>\r\n<div style=\"border:solid .1em #ccc;color:Black;background-color:White;margin:.25em 0.5em 0 0.5em;padding:0.25em 1.75em 0.25em 1.25em\">\r\n<pre>command.CommandText = sql;\r\n<span style=\"color:Blue\">using</span> (OracleDataReader reader = command.ExecuteReader())\r\n{\r\n   <span style=\"color:Blue\">while</span> (reader.Read())\r\n   {\r\n       payload = reader[<span style=\"color:#A31515\">&quot;Payload&quot;</span>].ToString();\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;payload.xml&quot;</span>, payload);\r\n           zip.AddFile(<span style=\"color:#A31515\">&quot;d:\\\\projects\\\\system\\\\Dashboard\\\\foo.txt&quot;</span>);\r\n           zip.Save(<span style=\"color:#A31515\">&quot;d:\\\\projects\\\\system\\\\Dashboard\\\\payload.zip&quot;</span>);\r\n         }\r\n     }\r\n}</pre>\r\n</div>",
    "PostedDate": "2010-04-06T11:24:04.113-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "427976",
    "ThreadId": "208465",
    "Html": "<blockquote style=\"border:solid .1em #ccc;font-style:italic;margin:.25em 1em 0 1em;padding:0 .25em 0 .25em\"><strong>Cheeso wrote:</strong><br>\r\n<p>But, you don't need the stream.&nbsp; Why not just use the ZipFile.AddEntry overload that accepts a string?</p>\r\n</blockquote>\r\n<p>...whoops lol.&nbsp;&nbsp; ...I skimmed through the documentation and saw that it would accept a Stream in lieu of a file and jumped too hard into that.&nbsp;</p>\r\n<p>...and &quot;oh yeah&quot; about the MemoryStream.&nbsp; I'd honestly completely forgotten that you have to 'reset' them like that (hadn't worked with a MemoryStream directly since probably my Sophomore year of school)</p>",
    "PostedDate": "2010-04-06T11:37:31.28-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]