[
  {
    "Id": "519648",
    "ThreadId": "234089",
    "Html": "\r\n<p>Hello</p>\r\n<p>I am using the AddDirectory method to add several folders to a zip file. I then save the zip file to the output stream. I have some users complaining of corrupt zip files.</p>\r\n<p>I also get the error when I&nbsp;manually test the app on the server, however, when I debug the code locally I cannot recreate the issue.</p>\r\n<p>I found a discussion thread that was on point&nbsp;(Thread&nbsp;64325). I am having the same type of issue, but I&nbsp;definitely never &quot;accidentally&quot; created a zip that I am adding to the new zip. After downloading, when using Windows standard unzip program,\r\n I will get a corrupt file error. If I use WinRar, I find a file with no file extension. If I rename it and give it a zip extension everything is there.</p>\r\n<p>Any ideas?</p>\r\n<p></p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:blue\">try</span>\r\n        {\r\n            Models.StoreOrder order = (Models.StoreOrder)Session[<span style=\"color:#a31515\">&quot;Order&quot;</span>];\r\n            BL.StoreManager Store = <span style=\"color:blue\">new</span> BL.StoreManager();\r\n\r\n\r\n            Response.Clear();\r\n            Response.BufferOutput = <span style=\"color:blue\">false</span>; <span style=\"color:green\">// no buffering - allows large zip files to download as they are zipped </span>\r\n            <span style=\"color:blue\">string</span> ReadmeText = Store.GetDownloadReadMe(order.ObjectsInOrder) &#43; <span style=\"color:#a31515\">&quot; &quot;</span> &#43; DateTime.Now.ToString(<span style=\"color:#a31515\">&quot;G&quot;</span>);\r\n            <span style=\"color:blue\">string</span> archiveName = String.Format(<span style=\"color:#a31515\">&quot;Wisc-Online-{0}.zip&quot;</span>, DateTime.Now.ToString(<span style=\"color:#a31515\">&quot;yyyy-MMM-dd-HHmmss&quot;</span>));\r\n            Response.ContentType = <span style=\"color:#a31515\">&quot;application/zip&quot;</span>;\r\n            Response.AddHeader(<span style=\"color:#a31515\">&quot;content-disposition&quot;</span>, <span style=\"color:#a31515\">&quot;attachment; filename=&quot;</span> &#43; archiveName);\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;Readme.txt&quot;</span>, ReadmeText);\r\n\r\n                <span style=\"color:blue\">foreach</span> (Models.Object o <span style=\"color:blue\">in</span> order.ObjectsInOrder)\r\n                {\r\n                    System.IO.DirectoryInfo dir = <span style=\"color:blue\">new</span> System.IO.DirectoryInfo(Server.MapPath(<span style=\"color:#a31515\">&quot;/Objects/&quot;</span> &#43; o.ID));\r\n                    zip.AddDirectory(dir.FullName, dir.Name);\r\n\r\n                }\r\n\r\n                zip.Save(Response.OutputStream);\r\n            }\r\n            Session[<span style=\"color:#a31515\">&quot;Order&quot;</span>] = <span style=\"color:blue\">null</span>;\r\n        }\r\n        <span style=\"color:blue\">catch</span> (Exception ex)\r\n        {\r\n            BL.SiteManager sm = <span style=\"color:blue\">new</span> BL.SiteManager();\r\n            sm.SendEmailToWebmaster(BL.SharedConstants.WEBMASTEREMAIL, ex.StackTrace.ToString() &#43; ex.Message, <span style=\"color:#a31515\">&quot;Download exception&quot;</span>);\r\n            <span style=\"color:blue\">throw</span>;\r\n        }\r\n        <span style=\"color:blue\">finally</span>\r\n        {\r\n            HttpContext.Current.ApplicationInstance.CompleteRequest();\r\n        }\r\n\r\n</pre>\r\n</div>\r\n<p></p>\r\n",
    "PostedDate": "2010-11-09T15:25:21.967-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "519715",
    "ThreadId": "234089",
    "Html": "\r\n<p>Yes.</p>\r\n<p>Try this:&nbsp; remove the call to HttpContext.Current.ApplicationInstance.CompleteRequest(), and replace it with Response.Close().</p>\r\n<p>There was a time when a sample shipped with that CompleteRequest in it, but that is wrong.&nbsp;I have since discovered that&nbsp;using it can result in corrupt zip files.&nbsp;You should use Response.Close() instead.</p>\r\n<p>&nbsp;</p>\r\n",
    "PostedDate": "2010-11-09T18:17:50.957-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "519720",
    "ThreadId": "234089",
    "Html": "\r\n<p>Thank you for replying. I use this library in a few apps and I really like working with it. &nbsp;In the other apps, I am zipping on the fly client side and uploading to the server. Works like a charm.</p>\r\n<p>&nbsp;</p>\r\n<p>Response.Close() resulted in a&nbsp;definite&nbsp;corrupt zip. This time around even WinRar throws an error when unzipping - unexpected end of archive. &nbsp;Could this be caused by the Response ending abrupt;y before the process is finished? It all happens\r\n on the same thread so I am not sure if that is possible. Perhaps I shouldn't make assumptions there. Is the process asynch?</p>\r\n<p>&nbsp;</p>\r\n<p>C:\\Users\\stulo.FVTC\\Desktop\\Wisc-Online-2010-Nov-09-202621.zip: Unexpected end of archive! &nbsp;\r\n<br>\r\nC:\\Users\\stulo.FVTC\\Desktop\\Wisc-Online-2010-Nov-09-202621.zip: CRC failed in Wisc-Online-2010-Nov-09-202621. The file is corrupt</p>\r\n<p>&nbsp;</p>\r\n<p>&nbsp;</p>\r\n",
    "PostedDate": "2010-11-09T18:36:19.277-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "520071",
    "ThreadId": "234089",
    "Html": "\r\n<p>Hmm, that's interesting.&nbsp; Reliable corruption using Response.Close().&nbsp;</p>\r\n<p>No, the Save process won't be asynchronous.&nbsp; When Save() returns, it's done.</p>\r\n<p>You could try Response.End() in place of Response.Close().&nbsp; You could also try Response.Flush before Response.Close().</p>\r\n<p>I don't know what else to suggest - try a few of these combinations and see if you can get it working. let me know.</p>\r\n<p>&nbsp;</p>\r\n",
    "PostedDate": "2010-11-10T09:28:40.857-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "520140",
    "ThreadId": "234089",
    "Html": "\r\n<p>After trying every possible combination of ending the response with or without flushing I ended up with variations of corruption.</p>\r\n<p>After a little digging, I found out that Response.Flush is only&nbsp;necessary&nbsp;if not&nbsp;buffering&nbsp;the response. After a bit more research, it&nbsp;occurred&nbsp;to me to setting BufferOutput to false may have been the cause of the issue. I commented\r\n out that line and the files downloaded intact. So as I understand it, now the user will have to wait for the entire zip to finish before the download begins. I can live with that.</p>\r\n<p>My theory is that somehow allowing the download to start as the zip was processed, caused the error. It know for sure now that it is all the same thread but... possibly a race condition?</p>\r\n<p>&nbsp;</p>\r\n",
    "PostedDate": "2010-11-10T11:32:07.983-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "520500",
    "ThreadId": "234089",
    "Html": "\r\n<p>I couldn't judge the cause unless I was able to see the zip files produced in each case. or reproduce it here.&nbsp; Your understanding is correct - buffering usually means the entire response is buffered before IIS begins to transmit the first byte to the\r\n requester. For a large zip this can be a large problem.&nbsp; For smaller zips, no problem.&nbsp; The other side effect is, of course, memory usage on the server. If you have many concurrent requests, they will ALL buffer their results before sending. This\r\n can cause memory usage to spike.&nbsp; Test to be sure. &nbsp;</p>\r\n<p>Regarding the differences in generating zips with buffering and without....Zips written to non-seekable streams use a slightly different format than those written to seekable streams. When you turn buffering off, Response.OutputStream becomes non-seekable,\r\n and DotNetZip uses the slightly different metadata format (described in some other locations as &quot;bit 3 encoding&quot; - check the zip spec for what this means). Some zip utilities do not properly read bit-3 encoded zip files.&nbsp; One notable example is, I think,\r\n the built-in zip handling in MacOs.&nbsp; It will&nbsp;classify such a zip file as corrupted, though other tools can read and extract the zip just fine. WinZip has never exhibited a problem handling bit-3 zip files, as far as I know.&nbsp; The DotNetZip tools\r\n and library can read zips with or without bit-3 encoding.</p>\r\n<p>if you're happy then I guess it's no problem.&nbsp;&nbsp; It's troubling to me that turning off buffering gives you corruption, and I'll want to investigate that for myself. But if you're satisfied then I guess this particular issue is closed.</p>\r\n<p>thanks for the report.</p>\r\n<p>&nbsp;</p>\r\n",
    "PostedDate": "2010-11-11T04:24:41.413-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "520502",
    "ThreadId": "234089",
    "Html": "This discussion has been copied to a work item. Click <a href=\"http://dotnetzip.codeplex.com/workitem/12466\">here</a> to go to the work item and continue the discussion.",
    "PostedDate": "2010-11-11T04:25:33.14-08:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]