SharpZipLib是国外开源加压解压库,可以方便的对文件进行加压/解压

1.下载ICSharpCode.SharpZipLib.dll,并复制到bin目录下

http://www.icsharpcode.net/OpenSource/SharpZipLib/Download.aspx

2.编写工具类ZipUtil,一般放在App_Code文件夹下

 1 using System;
 2 using System.Data;
 3 using System.Configuration;
 4 using System.Web;
 5 using System.Web.Security;
 6 using System.Web.UI;
 7 using System.Web.UI.WebControls;
 8 using System.Web.UI.WebControls.WebParts;
 9 using System.Web.UI.HtmlControls;
10 using ICSharpCode.SharpZipLib.Zip;
11 using System.IO;
12 /// <summary>
13 /// ZipUtil 压缩解压工具
14 /// </summary>
15 public class ZipUtil
16 {
17     public ZipUtil()
18     {
19         
20     }
21     /// <summary>
22     /// 压缩文件
23     /// </summary>
24     /// <param name="filename"></param>
25     /// <param name="directory"></param>
26     public static void PackFiles(string filename, string directory)
27     {
28         try
29         {
30             FastZip fz = new FastZip();
31             fz.CreateEmptyDirectories = true;
32             fz.CreateZip(filename, directory, true, "");
33             fz = null;
34         }
35         catch (Exception)
36         {
37             throw;
38         }
39     }
40     /// <summary>
41     /// 解压文件
42     /// </summary>
43     /// <param name="file">完整路径(包括文件名)</param>
44     /// <param name="dir">路径</param>
45     /// <returns></returns>
46     public static bool UnpackFiles(string file, string dir)
47     {
48 
49         try
50         {
51             if (!Directory.Exists(dir))
52                 Directory.CreateDirectory(dir);
53 
54             ZipInputStream s = new ZipInputStream(File.OpenRead(file));
55 
56             ZipEntry theEntry;
57             while ((theEntry = s.GetNextEntry()) != null)
58             {
59 
60                 string directoryName = Path.GetDirectoryName(theEntry.Name);
61                 string fileName = Path.GetFileName(theEntry.Name);
62 
63                 if (directoryName != String.Empty)
64                     Directory.CreateDirectory(dir + directoryName);
65 
66                 if (fileName != String.Empty)
67                 {
68                     FileStream streamWriter = File.Create(dir + theEntry.Name);
69 
70                     int size = 2048;
71                     byte[] data = new byte[2048];
72                     while (true)
73                     {
74                         size = s.Read(data, 0, data.Length);
75                         if (size > 0)
76                         {
77                             streamWriter.Write(data, 0, size);
78                         }
79                         else
80                         {
81                             break;
82                         }
83                     }
84 
85                     streamWriter.Close();
86                 }
87             }
88             s.Close();
89             return true;
90         }
91         catch (Exception)
92         {
93             throw;
94         }
95     }
96 }
View Code

相关文章:

  • 2021-12-01
  • 2022-12-23
  • 2021-12-15
  • 2022-12-23
  • 2022-01-29
  • 2021-12-14
  • 2021-05-27
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-01-01
  • 2021-12-25
  • 2022-01-29
  • 2022-12-23
  • 2021-07-26
  • 2022-01-24
相关资源
相似解决方案