{"id":655,"date":"2011-09-21T11:41:17","date_gmt":"2011-09-21T03:41:17","guid":{"rendered":"http:\/\/www.poloo.org\/?p=655"},"modified":"2011-09-21T11:41:17","modified_gmt":"2011-09-21T03:41:17","slug":"c-%e7%94%a8sharpziplib%e5%ae%9e%e7%8e%b0%e5%8a%a0%e5%af%86%e5%8e%8b%e7%bc%a9%e3%80%81%e5%8a%a0%e5%af%86%e8%a7%a3%e5%8e%8b","status":"publish","type":"post","link":"https:\/\/www.poloo.org\/?p=655","title":{"rendered":"C# \u7528SharpZipLib\u5b9e\u73b0\u52a0\u5bc6\u538b\u7f29\u3001\u52a0\u5bc6\u89e3\u538b"},"content":{"rendered":"<p>SharpZipLib \u662f\u4e00\u4e2a\u514d\u8d39\u7684Zip\u64cd\u4f5c\u7c7b\u5e93\uff0c\u53ef\u4ee5\u5229\u7528\u5b83\u5bf9 ZIP \u7b49\u591a\u79cd\u683c\u5f0f\u8fdb\u884c\u538b\u7f29\u4e0e\u89e3\u538b\u3002\u4e0b\u8f7d\u7f51\u5740\uff1a<a href=\"http:\/\/www.icsharpcode.net\/OpenSource\/SharpZipLib\/Download.aspx\">http:\/\/www.icsharpcode.net\/OpenSource\/SharpZipLib\/Download.aspx<\/a>\u3002\u76ee\u524d\u7684\u7248\u672c\u4e3a0.86\u3002\u00a0<\/p>\n<p align=\"left\"><strong><strong>1\u3001\u521b\u5efazip\u6587\u4ef6\uff0c\u5e76\u6dfb\u52a0\u6587\u4ef6\uff1a<\/strong><\/strong><\/p>\n<div>\n<div>using\u00a0(ZipFile\u00a0zip\u00a0=\u00a0ZipFile.Create(@&#8221;E:\\test.zip&#8221;))\u00a0<br \/>\n{\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.BeginUpdate();\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.Add(@&#8221;E:\\\u6587\u4ef61.txt&#8221;);\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.Add(@&#8221;E:\\\u6587\u4ef62.txt&#8221;);\u00a0<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.CommitUpdate();\u00a0<br \/>\n}\u00a0<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p align=\"left\"><strong><\/strong><strong>2\u3001\u5c06\u6587\u4ef6\u5939\u538b\u7f29\u4e3a\u6587\u4ef6<\/strong><\/p>\n<div>\n<div>(new\u00a0FastZip()).CreateZip(@&#8221;E:\\test.zip&#8221;,\u00a0@&#8221;E:\\test\\&#8221;,\u00a0true,\u00a0&#8220;&#8221;);<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p align=\"left\"><strong><\/strong>\u6700\u540e\u4e00\u4e2a\u53c2\u6570\u662f\u4f7f\u7528\u6b63\u5219\u8868\u8fbe\u5f0f\u8868\u793a\u7684\u8fc7\u6ee4\u6587\u4ef6\u89c4\u5219\u3002CreateZip\u65b9\u6cd5\u67093\u4e2a\u91cd\u8f7d\u7248\u672c\uff0c\u5176\u4e2d\u6709\u76ee\u5f55\u8fc7\u6ee4\u53c2\u6570\u3001\u6587\u4ef6\u8fc7\u6ee4\u53c2\u6570\u53ca\u7528\u4e8e\u6307\u5b9a\u662f\u5426\u8fdb\u884c\u5b50\u76ee\u5f55\u9012\u5f52\u7684\u4e00\u4e2abool\u7c7b\u578b\u7684\u53c2\u6570\u3002<\/p>\n<p align=\"left\"><strong>3\u3001\u5c06\u6587\u4ef6\u6dfb\u52a0\u5230\u5df2\u6709zip\u6587\u4ef6\u4e2d<\/strong><\/p>\n<div>\n<div>using\u00a0(ZipFile\u00a0zip\u00a0=\u00a0new\u00a0ZipFile(@&#8221;E:\\test.zip&#8221;))<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.BeginUpdate();<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.Add(@&#8221;E:\\test.doc&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.CommitUpdate();<br \/>\n}<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p align=\"left\"><strong><\/strong><strong>4\u3001\u5217\u51fazip\u6587\u4ef6\u4e2d\u6587\u4ef6<\/strong><\/p>\n<div>using\u00a0(ZipFile\u00a0zip\u00a0=\u00a0new\u00a0ZipFile(@&#8221;E:\\test.zip&#8221;))<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0\u00a0string\u00a0list\u00a0=\u00a0string.Empty;<br \/>\n\u00a0\u00a0\u00a0\u00a0foreach\u00a0(ZipEntry\u00a0entry\u00a0in\u00a0zip)<br \/>\n\u00a0\u00a0\u00a0\u00a0{<br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0list\u00a0+=\u00a0entry.Name\u00a0+\u00a0&#8220;\\r\\n&#8221;;<br \/>\n\u00a0\u00a0\u00a0\u00a0}<br \/>\n\u00a0\u00a0\u00a0\u00a0MessageBox.Show(list);<br \/>\n}<\/div>\n<p>&nbsp;<\/p>\n<p align=\"left\"><strong><\/strong><strong>5\u3001\u5220\u9664zip\u6587\u4ef6\u4e2d\u7684\u4e00\u4e2a\u6587\u4ef6<\/strong><\/p>\n<div>\n<div>using\u00a0(ZipFile\u00a0zip\u00a0=\u00a0new\u00a0ZipFile(@&#8221;E:\\test.zip&#8221;))<br \/>\n{<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.BeginUpdate();<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.Delete(@&#8221;test.doc&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.Delete(@&#8221;test22.txt&#8221;);<br \/>\n\u00a0\u00a0\u00a0\u00a0zip.CommitUpdate();<br \/>\n}<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p align=\"left\">\u00a0<strong>6\u3001\u89e3\u538bzip\u6587\u4ef6\u4e2d\u6587\u4ef6\u5230\u6307\u5b9a\u76ee\u5f55\u4e0b<\/strong><\/p>\n<div>\n<div>(new\u00a0FastZip()).ExtractZip(@&#8221;E:\\test.zip&#8221;,\u00a0@&#8221;E:\\test\\&#8221;,\u00a0&#8220;&#8221;);<\/div>\n<\/div>\n<p>&nbsp;<\/p>\n<p align=\"left\"><strong><\/strong><strong>7\u3001\u5e38\u7528\u7c7b<\/strong><\/p>\n<p align=\"left\"><strong><\/strong>ZipInputStream\u3001GZipInputStream\u7528\u4e8e\u89e3\u538b\u7f29Deflate\u3001GZip\u683c\u5f0f\u6d41\uff0cZipOutputStream\u3001GZipOutputStream\u7528\u4e8e\u538b\u7f29Deflate\u3001GZip\u683c\u5f0f\u6d41\u3002<br \/>\nStreamUtil\u7c7b\u5305\u542b\u4e86\u51e0\u4e2aStream\u5904\u7406\u8f85\u52a9\u65b9\u6cd5\uff1a<br \/>\n\u00a0 1) Copy(Stream, Stream, Byte[])\u7528\u4e8e\u4ece\u4e00\u4e2aStream\u5bf9\u8c61\u4e2d\u590d\u5236\u6570\u636e\u5230\u53e6\u4e00Stream\u5bf9\u8c61\u3002\u6709\u591a\u4e2a\u91cd\u5199\u3002<br \/>\n\u00a0 2) ReadFully(Stream, Byte [])\u7528\u4e8e\u4eceStream\u5bf9\u8c61\u4e2d\u8bfb\u53d6\u6240\u6709\u7684byte\u6570\u636e\u3002\u6709\u591a\u4e2a\u91cd\u5199\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4ee3\u7801\u5982\u4e0b\uff1a<\/p>\n<blockquote>\n<pre id=\"codeSnippet\">using System;\r\nusing System.Collections.Generic;\r\nusing System.Linq;\r\nusing System.Text;\r\nusing System.IO;\r\nusing System.Security.Cryptography;\r\nusing ICSharpCode.SharpZipLib.Checksums;\r\nusing ICSharpCode.SharpZipLib.Zip;\r\n\r\nnamespace XB.imp_exp\r\n{\r\n    class ZipClass\r\n    {\r\n        public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel, int BlockSize)\r\n        {\r\n            if (!System.IO.File.Exists(FileToZip))\r\n            {\r\n                throw new System.IO.FileNotFoundException(\"The specified file \" + FileToZip + \" could not be found. Zipping aborderd\");\r\n            }\r\n\r\n            System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);\r\n            System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);\r\n            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);\r\n            ZipEntry ZipEntry = new ZipEntry(\"ZippedFile\");\r\n            ZipStream.PutNextEntry(ZipEntry);\r\n            ZipStream.SetLevel(CompressionLevel);\r\n            byte[] buffer = new byte[BlockSize];\r\n            System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);\r\n            ZipStream.Write(buffer, 0, size);\r\n            try\r\n            {\r\n                while (size &lt; StreamToZip.Length)\r\n                {\r\n                    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);\r\n                    ZipStream.Write(buffer, 0, sizeRead);\r\n                    size += sizeRead;\r\n                }\r\n            }\r\n            catch (System.Exception ex)\r\n            {\r\n                throw ex;\r\n            }\r\n            ZipStream.Finish();\r\n            ZipStream.Close();\r\n            StreamToZip.Close();\r\n        }\r\n\r\n        public void ZipFileMain(string[] args, string password)\r\n        {\r\n            string[] filenames = Directory.GetFiles(args[0]);\r\n\r\n            ZipOutputStream s = new ZipOutputStream(File.Create(args[1]));\r\n\r\n            s.SetLevel(6); \/\/ 0 - store only to 9 - means best compression \r\n\r\n            s.Password = md5.encrypt(password);\r\n\r\n            foreach (string file in filenames)\r\n            {\r\n                \/\/\u6253\u5f00\u538b\u7f29\u6587\u4ef6 \r\n                FileStream fs = File.OpenRead(file);\r\n\r\n                byte[] buffer = new byte[fs.Length];\r\n                fs.Read(buffer, 0, buffer.Length);\r\n\r\n                Array arr = file.Split('\\\\');\r\n                string le = arr.GetValue(arr.Length - 1).ToString();\r\n                ZipEntry entry = new ZipEntry(le);\r\n                entry.DateTime = DateTime.Now;\r\n                entry.Size = fs.Length;\r\n                fs.Close();\r\n                s.PutNextEntry(entry);\r\n                s.Write(buffer, 0, buffer.Length);\r\n            }\r\n            s.Finish();\r\n            s.Close();\r\n        }\r\n    }\r\n\r\n    class UnZipClass\r\n    {\r\n        public void UnZip(string[] args, string password)\r\n        {\r\n            string directoryName = Path.GetDirectoryName(args[1]);\r\n            using (FileStream fileStreamIn = new FileStream(args[0], FileMode.Open, FileAccess.Read))\r\n            {\r\n                using (ZipInputStream zipInStream = new ZipInputStream(fileStreamIn))\r\n                {\r\n                    zipInStream.Password = md5.encrypt(password);\r\n                    ZipEntry entry = zipInStream.GetNextEntry();\r\n                    do\r\n                    {\r\n                        using (FileStream fileStreamOut = new FileStream(directoryName + @\"\\\" + entry.Name, FileMode.Create, FileAccess.Write))\r\n                        {\r\n\r\n                            int size = 2048;\r\n                            byte[] buffer = new byte[2048];\r\n                            do\r\n                            {\r\n                                size = zipInStream.Read(buffer, 0, buffer.Length);\r\n                                fileStreamOut.Write(buffer, 0, size);\r\n                            } while (size &gt; 0);\r\n                        }\r\n                    } while ((entry = zipInStream.GetNextEntry()) != null);\r\n                }\r\n            }\r\n        }\r\n    }\r\n\r\n    class md5\r\n    {\r\n        #region \"MD5\u52a0\u5bc6\"\r\n        \/\/\/ &lt;summary&gt;\r\n        \/\/\/32\u4f4d MD5\u52a0\u5bc6\r\n        \/\/\/ &lt;\/summary&gt;\r\n        \/\/\/ &lt;param name=\"str\"&gt;\u52a0\u5bc6\u5b57\u7b26&lt;\/param&gt;\r\n        \/\/\/ &lt;returns&gt;&lt;\/returns&gt;\r\n        public static string encrypt(string str)\r\n        {\r\n            string cl = str;\r\n            string pwd = \"\";\r\n            MD5 md5 = MD5.Create();\r\n            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));\r\n            for (int i = 0; i &lt; s.Length; i++)\r\n            {\r\n                pwd = pwd + s[i].ToString(\"X\");\r\n            }\r\n            return pwd;\r\n        }\r\n        #endregion\r\n    }\r\n\r\n}<\/pre>\n<p>&nbsp;<\/p><\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>SharpZipLib \u662f\u4e00\u4e2a\u514d\u8d39\u7684Zip\u64cd\u4f5c\u7c7b\u5e93\uff0c\u53ef\u4ee5\u5229\u7528\u5b83\u5bf9 Z&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13],"tags":[27,28,29,30],"class_list":["post-655","post","type-post","status-publish","format-standard","hentry","category-NET","tag-c","tag-zip","tag-29","tag-30"],"_links":{"self":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/655","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=655"}],"version-history":[{"count":1,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/655\/revisions"}],"predecessor-version":[{"id":656,"href":"https:\/\/www.poloo.org\/index.php?rest_route=\/wp\/v2\/posts\/655\/revisions\/656"}],"wp:attachment":[{"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=655"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=655"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.poloo.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=655"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}