DotNetZipLib类库的地址:http://dotnetzip.codeplex.com/
将压缩包解压引用 :
注意:以下所有代码都是控制台应用程序
先来一个测试(生成一个压缩包):
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ionic.Zip; namespace ZipKayer { public class Program { public static void Main() { Console.WriteLine("Press a path to zip by ENTER: "); string ZipFileToCreate = Console.ReadLine(); Console.WriteLine("Press a path to create by ENTER:"); string DirectoryToZip = Console.ReadLine(); try { using (ZipFile zip = new ZipFile()) { String[] filenames = System.IO.Directory.GetFiles(DirectoryToZip); foreach (String filename in filenames) { Console.WriteLine("Adding {0}...", filename); ZipEntry e = zip.AddFile(filename); e.Comment = "Added by Cheeso's CreateZip utility."; } zip.Comment = String.Format("This zip archive was created by the CreateZip example application on machine '{0}'", System.Net.Dns.GetHostName()); zip.Save(ZipFileToCreate); } } catch (System.Exception ex1) { Console.WriteLine("exception: " + ex1); } Console.Read(); } } }
代码解释:
①:ZipFileToCreate : 为生成Zip文件的字符串path
②:DirectoryToZip : 需要生成Zip的文件夹的Path
以上2个String变量都采取控制台输入方式
现测试如下:
运行程序:
运行的结果 , 会在相关目录下(ZipFileToCreate)生成一个Housing.zip的压缩文件
打开ZIP文件发现,Root是Happy(E盘以下的一个文件夹),而且中文的docx名称
现在看看如何解压:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using Ionic.Zip; namespace ZipKayer { public class Program { public static void Main() { try { ReadOptions options = new ReadOptions { StatusMessageWriter = System.Console.Out }; using (ZipFile zip = ZipFile.Read(@"E:\Happy\Housing\Housing.zip", options)) { zip.ExtractAll(@"E:\Happy"); } } catch (System.Exception ex1) { System.Console.Error.WriteLine("exception: " + ex1); } Console.Read(); } } }
解释:
①:@"E:\Happy\Housing\Housing.zip" -> 需要解压的Zip文件
②:@"E:\Happy" -> 解压后的文本存放的位置
所以 , 对于程序来说,没事不要取中文名字。