.NET文件压缩

以下为网上找的代码,具体出处忘记了

 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="list">待压缩的文件或目录集合</param>
51             /// <param name="strZipName">压缩后的文件名</param>
52             /// 删除文件
53             /// <param name="IsDirStruct">是否按目录结构压缩</param>
54             /// <returns>成功:true/失败:false</returns>
55             public static bool CompressMulti(List<string> list, string strZipName, bool IsDirStruct)
56             {
57                 try
58                 {
59                     using (ZipFile zip = new ZipFile(Encoding.Default))//设置编码,解决压缩文件时中文乱码
60                     {
61                         foreach (string path in list)
62                         {
63                             string fileName = Path.GetFileName(path);//取目录名称
64                             //如果是目录
65                             if (Directory.Exists(path))
66                             {
67                                 if (IsDirStruct)//按目录结构压缩
68                                 {
69                                     zip.AddDirectory(path, fileName);
70                                 }
71                                 else//目录下的文件都压缩到Zip的根目录
72                                 {
73                                     zip.AddDirectory(path);
74                                 }
75                             }
76                             if (File.Exists(path))//如果是文件
77                             {
78                                 zip.AddFile(path);
79                             }
80                         }
81                         zip.Save(strZipName);//压缩
82 
83 
84 
85                         return true;
86                     }
87                 }
88                 catch (Exception)
89                 {
90                     return false;
91                 }
92             }
93 
94         }

我用得是第二个,用来压缩文件集合了

 

posted @ 2017-03-20 16:09  fgnu  阅读(477)  评论(0编辑  收藏  举报