DotNetZip封装类

DotnetZip是一个开源类库,支持.NET的任何语言,可很方便的创建,读取,和更新zip文件。而且还可以使用在.NETCompact Framework中。

下载地址在这里:

http://dotnetzip.codeplex.com/

下载到的包里有很多个dll文件,一般引用Ionic.Zip.dll就可以:

然后引用这个命名空间:

using Ionic.Zip; 

以下是我自己封装的一个类:

  1     /// <summary>
  2     /// Zip操作基于 DotNetZip 的封装
  3     /// </summary>
  4     public static class ZipUtils
  5     {
  6         /// <summary>
  7         /// 得到指定的输入流的ZIP压缩流对象【原有流对象不会改变】
  8         /// </summary>
  9         /// <param name="sourceStream"></param>
 10         /// <returns></returns>
 11         public static Stream ZipCompress(Stream sourceStream, string entryName = "zip")
 12         {
 13             MemoryStream compressedStream = new MemoryStream();
 14             if (sourceStream != null)
 15             {
 16                 long sourceOldPosition = 0;
 17                 try
 18                 {
 19                     sourceOldPosition = sourceStream.Position;
 20                     sourceStream.Position = 0;
 21                     using (ZipFile zip = new ZipFile())
 22                     {
 23                         zip.AddEntry(entryName, sourceStream);
 24                         zip.Save(compressedStream);
 25                         compressedStream.Position = 0;
 26                     }
 27                 }
 28                 catch
 29                 {
 30                 }
 31                 finally
 32                 {
 33                     try
 34                     {
 35                         sourceStream.Position = sourceOldPosition;
 36                     }
 37                     catch
 38                     {
 39                     }
 40                 }
 41             }
 42             return compressedStream;
 43         }
 44 
 45 
 46         /// <summary>
 47         /// 得到指定的字节数组的ZIP解压流对象
 48         /// 当前方法仅适用于只有一个压缩文件的压缩包,即方法内只取压缩包中的第一个压缩文件
 49         /// </summary>
 50         /// <param name="sourceStream"></param>
 51         /// <returns></returns>
 52         public static Stream ZipDecompress(byte[] data)
 53         {
 54             Stream decompressedStream = new MemoryStream();
 55             if (data != null)
 56             {
 57                 try
 58                 {
 59                     MemoryStream dataStream = new MemoryStream(data);
 60                     using (ZipFile zip = ZipFile.Read(dataStream))
 61                     {
 62                         if (zip.Entries.Count > 0)
 63                         {
 64                             zip.Entries.First().Extract(decompressedStream);
 65                             // Extract方法中会操作ms,后续使用时必须先将Stream位置归零,否则会导致后续读取不到任何数据
 66                             // 返回该Stream对象之前进行一次位置归零动作
 67                             decompressedStream.Position = 0;
 68                         }
 69                     }
 70                 }
 71                 catch
 72                 {
 73                 }
 74             }
 75             return decompressedStream;
 76         }
 77 
 78         /// <summary>
 79         /// 压缩ZIP文件
 80         /// 支持多文件和多目录,或是多文件和多目录一起压缩
 81         /// </summary>
 82         /// <param name="list">待压缩的文件或目录集合</param>
 83         /// <param name="strZipName">压缩后的文件名</param>
 84         /// <param name="IsDirStruct">是否按目录结构压缩</param>
 85         /// <returns>成功:true/失败:false</returns>
 86         public static bool CompressMulti(List<string> list, string strZipName, bool IsDirStruct)
 87         {
 88             try
 89             {
 90                 using (ZipFile zip = new ZipFile(Encoding.Default))//设置编码,解决压缩文件时中文乱码
 91                 {
 92                     foreach (string path in list)
 93                     {
 94                         string fileName = Path.GetFileName(path);//取目录名称
 95                         //如果是目录
 96                         if (Directory.Exists(path))
 97                         {
 98                             if (IsDirStruct)//按目录结构压缩
 99                             {
100                                 zip.AddDirectory(path, fileName);
101                             }
102                             else//目录下的文件都压缩到Zip的根目录
103                             {
104                                 zip.AddDirectory(path);
105                             }
106                         }
107                         if (File.Exists(path))//如果是文件
108                         {
109                             zip.AddFile(path);
110                         }
111                     }
112                     zip.Save(strZipName);//压缩
113                     return true;
114                 }
115             }
116             catch (Exception)
117             {
118                 return false;
119             }
120         }
121 
122         /// <summary>
123         /// 解压ZIP文件
124         /// </summary>
125         /// <param name="strZipPath">待解压的ZIP文件</param>
126         /// <param name="strUnZipPath">解压的目录</param>
127         /// <param name="overWrite">是否覆盖</param>
128         /// <returns>成功:true/失败:false</returns>
129         public static bool Decompression(string strZipPath, string strUnZipPath, bool overWrite)
130         {
131             try
132             {
133                 ReadOptions options = new ReadOptions();
134                 options.Encoding = Encoding.Default;//设置编码,解决解压文件时中文乱码
135                 using (ZipFile zip = ZipFile.Read(strZipPath, options))
136                 {
137                     foreach (ZipEntry entry in zip)
138                     {
139                         if (string.IsNullOrEmpty(strUnZipPath))
140                         {
141                             strUnZipPath = strZipPath.Split('.').First();
142                         }
143                         if (overWrite)
144                         {
145                             entry.Extract(strUnZipPath, ExtractExistingFileAction.OverwriteSilently);//解压文件,如果已存在就覆盖
146                         }
147                         else
148                         {
149                             entry.Extract(strUnZipPath, ExtractExistingFileAction.DoNotOverwrite);//解压文件,如果已存在不覆盖
150                         }
151                     }
152                     return true;
153                 }
154             }
155             catch (Exception)
156             {
157                 return false;
158             }
159         }
160 
161 
162     }

 

使用方法:

1.压缩文件  

  List<string> list = new List<string>();
  list.Add(@"D:\Test\ss");
  list.Add(@"D:\Test\test1.jpg");
  list.Add(@"D:\公司文件.txt");
  list.Add(@"D:\Test\ss.xml"); 
  bool isSuc =ZipUtils. CompressMulti(list, "D:\\Test2.zip",true);

 2.解压文件 

  bool isSuc = ZipUtils.Decompression("D:\\Test\\Test1.zip""D:\\Teest"true); 

 

 

 

更详细的例子在这里:

 

http://dotnetzip.codeplex.com/wikipage?title=Examples&referringTitle=Home

posted @ 2013-10-12 16:48  weixiao520  阅读(1912)  评论(2编辑  收藏  举报