{"id":1463,"date":"2013-05-01T10:33:44","date_gmt":"2013-05-01T09:33:44","guid":{"rendered":"http:\/\/www.stuartroberts.net\/?p=1463"},"modified":"2013-10-19T17:27:34","modified_gmt":"2013-10-19T16:27:34","slug":"quick-tip-17","status":"publish","type":"post","link":"http:\/\/www.stuartroberts.net\/index.php\/2013\/05\/01\/quick-tip-17\/","title":{"rendered":"Create Zip File"},"content":{"rendered":"<p><strong>SharePoint Short #17<\/strong><\/p>\n<p>Not so much a SharePoint post, but can be used in a SharePoint project, so counts for me \ud83d\ude42<\/p>\n<p>Using the WindowsBase assembly provides access to the System.IO.Packaging namespace and the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.packaging.packagepart.aspx\" title=\"PackagePart Class\" target=\"_blank\">PackagePart<\/a> class.<\/p>\n<p>Using this class to create an in memory zip file containing one or more files is pretty straight forward:<\/p>\n<pre lang=\"csharp\">\r\npublic byte[] GetZipData()\r\n{\r\n    byte[] data;\r\n    using (MemoryStream stream = new MemoryStream())\r\n    {\r\n        using (Package package = ZipPackage.Open(stream, FileMode.Create, FileAccess.ReadWrite))\r\n        {\r\n            \/\/ Wrap the rest of the using block in a for loop to iterate over files you want to\r\n            \/\/ include in the package, this example only adds a single file.\r\n            \r\n            \/\/ Create the file to include in the zip\r\n            Uri uri = PackUriHelper.CreatePartUri(new Uri(\"FileToIncludeInZip.txt\", UriKind.Relative));\r\n            PackagePart packagePart = package.CreatePart(uri, MediaTypeNames.Application.Octet, CompressionOption.Normal);\r\n            \r\n            using (Stream packagePartStream = packagePart.GetStream())\r\n            {\r\n                \/\/ Get content for file as byte array.\r\n                byte[] packagePartData = Encoding.UTF8.GetBytes(\"content to write to FileToIncludeInZip.txt file\");\r\n                \/\/ Commit to package\r\n                packagePartStream.Write(packagePartData, 0, packagePartData.Length);\r\n            }\r\n        }\r\n\r\n        \/\/ Write stream content to data byte array\r\n        data = new byte[stream.Length];\r\n        stream.Position = 0;\r\n        stream.Read(data, 0, Convert.ToInt32(stream.Length));\r\n    }\r\n\r\n    return data;\r\n}\r\n\r\n\/\/ Code in you aspx page, WebPart, control, etc. :\r\n\r\nbyte[] data = GetZipData();\r\n\r\nPage.Response.Buffer = true;\r\nPage.Response.ClearHeaders();\r\nPage.Response.ClearContent();\r\nPage.Response.AddHeader(\"Content-Length\", data.Length.ToString());\r\nPage.Response.ContentType = \"application\/zip\";\r\nPage.Response.AppendHeader(\"Content-Disposition\", \"attachment;filename=DownloadableZipFile.zip\");\r\nPage.Response.BinaryWrite(data);\r\nPage.Response.Flush();\r\nPage.Response.Close();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>SharePoint Short #17 Not so much a SharePoint post, but can be used in a SharePoint project, so counts for me \ud83d\ude42 Using the WindowsBase assembly provides access to the System.IO.Packaging namespace and the PackagePart class. Using this class to &hellip; <a href=\"http:\/\/www.stuartroberts.net\/index.php\/2013\/05\/01\/quick-tip-17\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_mi_skip_tracking":false,"jetpack_post_was_ever_published":false,"jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":[]},"categories":[44],"tags":[45],"jetpack_publicize_connections":[],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/plx2I-nB","_links":{"self":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1463"}],"collection":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/comments?post=1463"}],"version-history":[{"count":6,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1463\/revisions"}],"predecessor-version":[{"id":1567,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/posts\/1463\/revisions\/1567"}],"wp:attachment":[{"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/media?parent=1463"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/categories?post=1463"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stuartroberts.net\/index.php\/wp-json\/wp\/v2\/tags?post=1463"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}