{"id":160,"date":"2011-06-16T13:56:18","date_gmt":"2011-06-16T03:56:18","guid":{"rendered":"http:\/\/www.reenadu.com\/?p=160"},"modified":"2011-06-16T13:56:18","modified_gmt":"2011-06-16T03:56:18","slug":"zipping-the-download-files-on-the-fly-asp-net-real-time-zip-and-download-file-from-server","status":"publish","type":"post","link":"https:\/\/nickdu.com\/?p=160","title":{"rendered":"Zipping the download files on the fly ASP.Net (real-time zip and download file from server)"},"content":{"rendered":"<p>I am currently working on an ASP.Net web project, which allows client to download a well structured submission package. Due to security reason, web server can not create folder and write file on client side. Web server can generate a well structured zipped file and push to the client.<\/p>\n<p>There could be two ways to achieve that;<\/p>\n<p>Fist way:<\/p>\n<ol>\n<li>Create folders and copy files      into each folder<\/li>\n<li>Use 3<sup>rd<\/sup> party tool,      eg winrar, 7zip to zip the whole folder to one zipped file<\/li>\n<li>Send back download web address,      and let client download the zipped file<\/li>\n<\/ol>\n<p>If the zipped file is small and web server is powerful enough, client won\u2019t feel any delay. However if the file is big, client side will get no any response until zip file is ready. We can put similar message, eg \u201cPlease wait\u2026 \u201d. But client side still don\u2019t know how long would take. If zip file is not ready within time out period, server will get reset. This is a really awful user experience.<\/p>\n<p>Second way:<\/p>\n<ol>\n<li>Create folder and zip the files      on the fly (on web server\u2019s memory)<\/li>\n<li>Send back zipped stream to      client through <em>Response<\/em><\/li>\n<li>Client download \u00a0zipping file one by one<\/li>\n<\/ol>\n<p>Client can clearly feel the progress of downloading.<\/p>\n<p>Here is the code for 2<sup>nd<\/sup> solution:<\/p>\n<pre lang=\"csharp\">\n\/\/prepare response header\nResponse.Clear();\n\/\/set Response Content Type\nResponse.ContentType = \"application\/x-zip-compressed\";\nResponse.AddHeader(\"Content-Disposition\", \"attachment; filename=Download.zip\");\nZipOutputStream zipOutput = new ZipOutputStream(Response.OutputStream);\ntry\n{\n            zipOutput.IsStreamOwner = false;\n            \/\/set compress level\n            zipOutput.SetLevel(9);\n\n            ArrayList fileList = functions.GetFileList();\n            foreach (string fileName in fileList)\n            {\n                string folderName = \"folder1\" + @\"\";\n\n                zipEntry = new ZipEntry(folderName + fileName);\n                zipOutput.PutNextEntry(zipEntry);\n                            \n                byte[] file = functions.GetFile(fileName);\n                \/\/zip file\n                zipOutput.Write(file, 0, file.Length);\n                 \/\/send to client for downloading\n                Response.Flush();\n            }\n        }\n        catch (Exception ex)\n        {\n            Response.Write(ex.ToString());\n        }\n        finally\n        {\n            zipOutput.Finish();\n            zipOutput.Close();\n            Response.End();\n        }\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>I am currently working on an ASP.Net web project, which allows client to download a well structured submission package. Due to security reason, web server can not create folder and write file on client side. Web server can generate a well structured zipped file and push to the client. There could be two ways to &hellip; <a href=\"https:\/\/nickdu.com\/?p=160\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Zipping the download files on the fly ASP.Net (real-time zip and download file from server)&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9,2],"tags":[],"class_list":["post-160","post","type-post","status-publish","format-standard","hentry","category-net","category-it"],"_links":{"self":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/160","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=160"}],"version-history":[{"count":0,"href":"https:\/\/nickdu.com\/index.php?rest_route=\/wp\/v2\/posts\/160\/revisions"}],"wp:attachment":[{"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=160"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=160"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nickdu.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=160"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}