[
  {
    "Id": "619207",
    "ThreadId": "259231",
    "Html": "\r\n<p>I'm trying to create and return a dotnetzip generated zip as an ActionResult in Asp.net MVC:</p>\r\n<p>&nbsp;</p>\r\n<div style=\"color:black; background-color:white\">\r\n<pre style=\"color:black\"></pre>\r\n<div style=\"color:black; background-color:white\">\r\n<pre><span style=\"color:blue\">public</span> ActionResult DownloadZip()\r\n{\r\n\t<span style=\"color:blue\">using</span> (<span style=\"color:blue\">var</span> zip = <span style=\"color:blue\">new</span> ZipFile())\r\n\t{\r\n\t\t<span style=\"color:blue\">using</span> (<span style=\"color:blue\">var</span> mstream = <span style=\"color:blue\">new</span> MemoryStream())\r\n<span style=\"color:black; white-space:pre\">\t\t</span>{\r\n<span style=\"color:blue\"><span style=\"white-space:pre\">\t\t\t</span>using</span>(<span style=\"color:blue\">var</span> writer = <span style=\"color:blue\">new</span> StreamWriter(mstream, Encoding.Default))\r\n<span style=\"white-space:pre\">\t\t\t</span>{\r\n<span style=\"white-space:pre\">\t\t\t\t</span>writer.Write(file.ToString());\r\n<span style=\"white-space:pre\">\t\t\t</span>}\r\n<span style=\"white-space:pre\">\t\t\t</span>zip.AddEntry(<span style=\"color:#a31515\">&quot;someFile.txt&quot;</span>, mstream);\r\n<span style=\"white-space:pre\">\t\t</span>}\r\n<span style=\"white-space:pre\">\t\t</span>zip.AddDirectory(Server.MapPath(<span style=\"color:#a31515\">&quot;~/NonWeb/someFolder&quot;</span>));\r\n<span style=\"color:blue\"><span style=\"white-space:pre\">\t\t</span>return</span> <span style=\"color:blue\">new</span> FileStreamResult(zip.GetStream(), <span style=\"color:#a31515\">&quot;application/octet-stream&quot;</span>);\r\n<span style=\"white-space:pre\">\t</span>}\r\n}\r\n</pre>\r\n</div>\r\n<pre style=\"color:black\"></pre>\r\n</div>\r\n<p>However there doesn't appear to be anything like a &quot;GetStream()&quot; method, is there something else I can do to achieve this or does the zip file have to be written to the file system first in order to grab a stream?</p>\r\n",
    "PostedDate": "2011-05-27T03:02:43.763-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "619223",
    "ThreadId": "259231",
    "Html": "<p>FWIW I've tried this:</p>\r\n<p>\r\n<div style=\"color: black; background-color: white;\">\r\n<pre><span style=\"color: blue;\">public</span> <span style=\"color: blue;\">class</span> ZipResult : ActionResult\r\n    {\r\n        <span style=\"color: blue;\">private</span> ZipFile _zipFile;\r\n        <span style=\"color: blue;\">private</span> <span style=\"color: blue;\">string</span> _filename;\r\n\r\n        <span style=\"color: blue;\">public</span> ZipResult(ZipFile zipFile, <span style=\"color: blue;\">string</span> filename)\r\n        {\r\n            _zipFile = zipFile;\r\n            _filename = filename;\r\n        }\r\n\r\n        <span style=\"color: blue;\">public</span> <span style=\"color: blue;\">override</span> <span style=\"color: blue;\">void</span> ExecuteResult(ControllerContext context)\r\n        {\r\n            <span style=\"color: blue;\">if</span> (_zipFile == <span style=\"color: blue;\">null</span>) <span style=\"color: blue;\">return</span>;\r\n\r\n            context.HttpContext.Response.ContentType = <span style=\"color: #a31515;\">\"application/octet-stream\"</span>;\r\n            context.HttpContext.Response.AddHeader(<span style=\"color: #a31515;\">\"content-disposition\"</span>, <span style=\"color: #a31515;\">\"attachment; filename=\"</span> + _filename);\r\n            <span style=\"color: green;\">// doesn't work</span>\r\n            _zipFile.Save(context.HttpContext.Response.OutputStream);\r\n\r\n        }\r\n    }\r\n</pre>\r\n</div>\r\nBut the Save method errors with an ObjectDisposedException \"Cannot access a closed Stream.\"</p>",
    "PostedDate": "2011-05-27T03:36:28.79-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "619318",
    "ThreadId": "259231",
    "Html": "<p>Got there in the end with the help of <a href=\"http://stackoverflow.com/questions/838397/stream-zip-file-mvc-net-start-streaming\">this stackoverflow</a> post, for the benefit of anyone else wanting to do this:</p>\r\n<p>\r\n<div style=\"color: black; background-color: white;\">\r\n<pre><span style=\"color: blue;\">public</span> <span style=\"color: blue;\">class</span> ZipResult : ActionResult\r\n    {\r\n        <span style=\"color: blue;\">private</span> <span style=\"color: blue;\">readonly</span> <span style=\"color: blue;\">string</span>[] _filenames;\r\n        <span style=\"color: blue;\">private</span> <span style=\"color: blue;\">readonly</span> <span style=\"color: blue;\">byte</span>[][] _files;\r\n        <span style=\"color: blue;\">private</span> <span style=\"color: blue;\">readonly</span> <span style=\"color: blue;\">string</span> _filename;\r\n\r\n        <span style=\"color: gray;\">///</span> <span style=\"color: gray;\">&lt;summary&gt;</span>\r\n        <span style=\"color: gray;\">///</span><span style=\"color: green;\"> Creates a streamed zip file for download</span>\r\n        <span style=\"color: gray;\">///</span> <span style=\"color: gray;\">&lt;/summary&gt;</span>\r\n        <span style=\"color: gray;\">///</span> <span style=\"color: gray;\">&lt;param name=\"filenames\"&gt;</span><span style=\"color: green;\">string array of file and foldernames to include in the zip file, folders simply end with a trailing slash (\"/\" or \"\\\")&lt;/param&gt;</span>\r\n        <span style=\"color: gray;\">///</span> <span style=\"color: gray;\">&lt;param name=\"files\"&gt;</span><span style=\"color: green;\">byte array of files must be in same order as files in filenames array (folders are skipped)&lt;/param&gt;</span>\r\n        <span style=\"color: gray;\">///</span> <span style=\"color: gray;\">&lt;param name=\"filename\"&gt;</span><span style=\"color: green;\">default filename for the zip file downloaded&lt;/param&gt;</span>\r\n        <span style=\"color: blue;\">public</span> ZipResult(<span style=\"color: blue;\">string</span>[] filenames, <span style=\"color: blue;\">byte</span>[][] files, <span style=\"color: blue;\">string</span> filename)\r\n        {\r\n            _filenames = filenames;\r\n            _files = files;\r\n            _filename = filename;\r\n        }\r\n\r\n        <span style=\"color: blue;\">public</span> <span style=\"color: blue;\">override</span> <span style=\"color: blue;\">void</span> ExecuteResult(ControllerContext context)\r\n        {\r\n            <span style=\"color: blue;\">if</span> (_files == <span style=\"color: blue;\">null</span>) <span style=\"color: blue;\">return</span>;\r\n\r\n            <span style=\"color: blue;\">var</span> response = context.HttpContext.Response;\r\n            response.Clear();\r\n            response.ClearContent();\r\n            response.ClearHeaders();\r\n            response.Cookies.Clear();\r\n            response.ContentType = <span style=\"color: #a31515;\">\"application/octet-stream\"</span>;\r\n            response.ContentEncoding = Encoding.Default;\r\n            response.AddHeader(<span style=\"color: #a31515;\">\"Content-Type\"</span>, <span style=\"color: #a31515;\">\"application/octet-stream\"</span>);\r\n            response.BufferOutput = <span style=\"color: blue;\">false</span>;\r\n            context.HttpContext.Response.AddHeader(<span style=\"color: #a31515;\">\"Content-Disposition\"</span>, String.Format(<span style=\"color: #a31515;\">\"attachment; filename={0}\"</span>, _filename));\r\n\r\n            <span style=\"color: blue;\">using</span> (<span style=\"color: blue;\">var</span> zipOStream = <span style=\"color: blue;\">new</span> ZipOutputStream(context.HttpContext.Response.OutputStream))\r\n            {\r\n                <span style=\"color: blue;\">var</span> index = 0;\r\n                <span style=\"color: blue;\">foreach</span>(<span style=\"color: blue;\">var</span> currentFileName <span style=\"color: blue;\">in</span> _filenames)\r\n                {\r\n                    <span style=\"color: green;\">// add filename to stream</span>\r\n                    zipOStream.PutNextEntry(currentFileName);\r\n\r\n                    <span style=\"color: green;\">// skip folders</span>\r\n                    <span style=\"color: blue;\">if</span>(currentFileName.EndsWith(<span style=\"color: #a31515;\">\"/\"</span>) || currentFileName.EndsWith(\"\\\\\"))\r\n                       <span style=\"color: blue;\">continue</span>;\r\n\r\n                    <span style=\"color: green;\">// write file contents</span>\r\n                    zipOStream.Write(_files[index], 0, _files[index].Length);\r\n                    index++;\r\n                }\r\n                zipOStream.Flush();\r\n            }\r\n            response.OutputStream.Flush();\r\n        }\r\n    }\r\n</pre>\r\n</div>\r\n</p>",
    "PostedDate": "2011-05-27T06:42:04.297-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  },
  {
    "Id": "619502",
    "ThreadId": "259231",
    "Html": "<p>Glad you worked it out.</p>",
    "PostedDate": "2011-05-27T11:50:51.393-07:00",
    "UserRole": null,
    "MarkedAsAnswerDate": null
  }
]