[
  {
    "Id": "447792",
    "ThreadId": "213959",
    "Html": "<p>Hi, I have a code that generates XLS/CSV/XLSX file.<br>The code sends the file using ms.writeto(response.outputstream) to the client as a download dialog box.<br><br>I wanted to add a feature to download a compressed version of the file, so instead of sending the stream of the file, I zip it first and then send the stream of the zip file instead of the original XLS file.<br><br>I have been trying to do that but for some reason, using the code below, I get a ZIP file with an empty XLS file inside.<br><br>Please help me, I have been trying to solve it for few hours with no luck at all.&nbsp; <br><br>Thanks,</p>\r\n<p>Lior<br><br>Response.ContentType = &quot;application/x-zip-compressed&quot;<br><br>Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + reportName + &quot;.zip&quot;)</p>\r\n<p>Dim ms As MemoryStream = New MemoryStream</p>\r\n<p>If FileFormat = &quot;CSV&quot; Then</p>\r\n<p>ef.SaveCsv(ms, CsvType.CommaDelimited)</p>\r\n<p>ElseIf FileFormat = &quot;XLS&quot; Then</p>\r\n<p>ef.SaveXls(ms)</p>\r\n<p>ElseIf FileFormat = &quot;XLSX&quot; Then</p>\r\n<p>ef.SaveXlsx(ms)</p>\r\n<p>End If</p>\r\n<p>ef.SaveXlsx(ms)</p>\r\n<p>Using zip As New ZipFile()</p>\r\n<p>zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression</p>\r\n<p>Dim entry As ZipEntry = zip.AddEntry(&quot;report.xls&quot;, ms)<br><br>entry.SetEntryTimes(Now(), Now(), Now())</p>\r\n<p>zip.Save(Response.OutputStream)</p>\r\n<p>End Using</p>",
    "PostedDate": "2010-05-26T11:54:30.183-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "447931",
    "ThreadId": "213959",
    "Html": "<p>add</p>\r\n<pre>ms.Seek(0, SeekOrigin.Begin)\r\n</pre>\r\n<p>before you call</p>\r\n<pre>using zip as New ZipFile()\r\n</pre>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-05-26T19:34:43.63-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "447964",
    "ThreadId": "213959",
    "Html": "<p>Worked like a charm.<br><br>Thank you very much Cheeso. :)</p>",
    "PostedDate": "2010-05-26T22:12:06.057-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "448114",
    "ThreadId": "213959",
    "Html": "<p>You could simplify the whole thing and eliminate the use of the temporary MemoryStream by using the <a href=\"http://cheeso.members.winisp.net/DotNetZipHelp/html/e20db3f7-587b-2457-3ea5-d25e7b1bf68d.htm\">WriteDelegate syntax of AddEntry</a>. It was designed specifically for your situation.</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><span style=\"color:blue\">Private</span> <span style=\"color:blue\">Sub</span> WriteEntry (<span style=\"color:blue\">ByVal</span> filename <span style=\"color:blue\">As</span> <span style=\"color:blue\">String</span>, <span style=\"color:blue\">ByVal</span> output <span style=\"color:blue\">As</span> Stream) \r\n    <span style=\"color:blue\">If</span> FileFormat = <span style=\"color:#a31515\">&quot;CSV&quot;</span> <span style=\"color:blue\">Then</span>\r\n        ef.SaveCsv(stream, CsvType.CommaDelimited)\r\n    <span style=\"color:blue\">ElseIf</span> FileFormat = <span style=\"color:#a31515\">&quot;XLS&quot;</span> <span style=\"color:blue\">Then</span>\r\n        ef.SaveXls(stream)\r\n    <span style=\"color:blue\">ElseIf</span> FileFormat = <span style=\"color:#a31515\">&quot;XLSX&quot;</span> <span style=\"color:blue\">Then</span>\r\n        ef.SaveXlsx(stream)\r\n    <span style=\"color:blue\">End</span> <span style=\"color:blue\">If</span>\r\n<span style=\"color:blue\">End</span> <span style=\"color:blue\">Sub</span>\r\n\r\n<span style=\"color:blue\">Public</span> <span style=\"color:blue\">Sub</span> Run()\r\n\r\n    Response.ContentType = <span style=\"color:#a31515\">&quot;application/x-zip-compressed&quot;</span>\r\n    Response.AddHeader(<span style=\"color:#a31515\">&quot;Content-Disposition&quot;</span>, <span style=\"color:#a31515\">&quot;attachment; filename=&quot;</span> + reportName + <span style=\"color:#a31515\">&quot;.zip&quot;</span>)\r\n    <span style=\"color:blue\">Using</span> zip <span style=\"color:blue\">As</span> <span style=\"color:blue\">New</span> ZipFile()\r\n        zip.CompressionLevel = Ionic.Zlib.CompressionLevel.BestCompression\r\n        <span style=\"color:blue\">Dim</span> entry <span style=\"color:blue\">As</span> ZipEntry = _\r\n            zip.AddEntry(<span style=\"color:#a31515\">&quot;report.xls&quot;</span>, <span style=\"color:blue\">New</span> WriteDelegate(<span style=\"color:blue\">AddressOf</span> WriteEntry))\r\n        entry.SetEntryTimes(Now(), Now(), Now())\r\n        zip.Save(Response.OutputStream)\r\n    <span style=\"color:blue\">End</span> <span style=\"color:blue\">Using</span>\r\n\r\n<span style=\"color:blue\">End</span> <span style=\"color:blue\">Sub</span>\r\n</pre>\r\n</div>\r\n<p>But your original approach will work fine.</p>",
    "PostedDate": "2010-05-27T04:34:13.05-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "450204",
    "ThreadId": "213959",
    "Html": "<p>I'm having a similar problem.&nbsp; I use the following function to zip a set of files and return a MemoryStream to an Attachment object to be added to an Email object.&nbsp; The zip file attaches OK but when I open it I find that it is empty.&nbsp; If I Save() the zip file to a directory then it contains the files.&nbsp; Any ideas?</p>\r\n<div style=\"color:black;background-color:white\">\r\n<pre>        <span style=\"color:blue\">public</span> Stream ZipFiles(<span style=\"color:blue\">string</span>[] files)\r\n        {\r\n            MemoryStream stream = <span style=\"color:blue\">new</span> MemoryStream();\r\n            stream.Seek(0, SeekOrigin.Begin);\r\n            <span style=\"color:blue\">using</span> (ZipFile zip = <span style=\"color:blue\">new</span> ZipFile())\r\n            {\r\n                <span style=\"color:blue\">foreach</span> (String filename <span style=\"color:blue\">in</span> files)\r\n                {\r\n                    ZipEntry e = zip.AddFile(filename);\r\n                }              \r\n                <span style=\"color:green\">//zip.Save(@&quot;test.zip&quot;);</span>\r\n                zip.Save(stream);\r\n            }\r\n            <span style=\"color:blue\">return</span> stream;\r\n        }\r\n</pre>\r\n</div>\r\n<p>Thanks!</p>\r\n<p>Ian</p>",
    "PostedDate": "2010-06-01T11:55:06.637-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "450211",
    "ThreadId": "213959",
    "Html": "<p>yes - Your .Seek() should come AFTER you save the zipfile to the stream.</p>",
    "PostedDate": "2010-06-01T12:10:09.677-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "450226",
    "ThreadId": "213959",
    "Html": "<p>That did it.&nbsp; Thanks, Cheeso!&nbsp; What threw me off was your first comment at the top of this thread.&nbsp; I read it again and it still seems to me that it suggests putting the Seek()&nbsp;before the &quot;using&quot; which would also put it before the Save().&nbsp; Hopefully this will help others from getting confused also.</p>\r\n<p>Thanks for the great utility!</p>\r\n<p>Ian</p>",
    "PostedDate": "2010-06-01T12:38:00.563-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "450243",
    "ThreadId": "213959",
    "Html": "<p>Well in the situation at the top of the thread, the memorystream was the *source* of the data or a ZipEntry.&nbsp;</p>\r\n<p>In your case the MemoryStream was the *destination* of the ZipFile.</p>\r\n<p>In either case, the situation was similar:&nbsp; after writing to the MemoryStream, you must invoke MemoryStream.Seek() before trying to Read it.</p>\r\n<ul>\r\n<li>In the first case, the Read() of the MemoryStream occured during ZipFile.Save(), therefore the MemoryStream.Seek() must be placed before ZipFile.Save().&nbsp; </li>\r\n<li>In your case, the Read of the MemoryStream occurred after ZipFile.Save() wrote to the MemoryStream, therefore the MemoryStream.Seek() must be placed after ZipFile.Save().&nbsp; </li>\r\n</ul>\r\n<p>&nbsp;</p>",
    "PostedDate": "2010-06-01T12:59:30.87-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]