C#解压缩DotNetZip
DotNetZipLib类库的地址:http://dotnetzip.codeplex.com/
将压缩包解压引用 :
注意:以下所有代码都是控制台应用程序
先来一个测试(生成一个压缩包):
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingIonic.Zip;namespaceZipKayer{publicclassProgram{publicstaticvoidMain(){Console.WriteLine("PressapathtozipbyENTER:");stringZipFileToCreate=Console.ReadLine();Console.WriteLine("PressapathtocreatebyENTER:");stringDirectoryToZip=Console.ReadLine();try{using(ZipFilezip=newZipFile()){String[]filenames=System.IO.Directory.GetFiles(DirectoryToZip);foreach(Stringfilenameinfilenames){Console.WriteLine("Adding{0}...",filename);ZipEntrye=zip.AddFile(filename);e.Comment="AddedbyCheeso'sCreateZiputility.";}zip.Comment=String.Format("ThisziparchivewascreatedbytheCreateZipexampleapplicationonmachine'{0}'",System.Net.Dns.GetHostName());zip.Save(ZipFileToCreate);}}catch(System.Exceptionex1){Console.WriteLine("exception:"+ex1);}Console.Read();}}}
代码解释:
①:ZipFileToCreate : 为生成Zip文件的字符串path
②:DirectoryToZip : 需要生成Zip的文件夹的Path
以上2个String变量都采取控制台输入方式
现测试如下:
运行程序:
运行的结果 , 会在相关目录下(ZipFileToCreate)生成一个Housing.zip的压缩文件
打开ZIP文件发现,Root是Happy(E盘以下的一个文件夹),而且中文的docx名称
现在看看如何解压:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingIonic.Zip;namespaceZipKayer{publicclassProgram{publicstaticvoidMain(){try{ReadOptionsoptions=newReadOptions{StatusMessageWriter=System.Console.Out};using(ZipFilezip=ZipFile.Read(@"E:\Happy\Housing\Housing.zip",options)){zip.ExtractAll(@"E:\Happy");}}catch(System.Exceptionex1){System.Console.Error.WriteLine("exception:"+ex1);}Console.Read();}}}
解释:
①:@"E:\Happy\Housing\Housing.zip"-> 需要解压的Zip文件
②:@"E:\Happy" -> 解压后的文本存放的位置
所以 , 对于程序来说,没事不要取中文名字。
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。