.NET使用ICSharpCode.SharpZipLib压缩/解压文件

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

3.编写HTML页面

我们上传文件使用FileUpload控件(最大支持20M上传)和一个Button按钮。

1 <asp:FileUpload ID="upZip" runat="server" Width="200px" />
2 <asp:Label ID="lblMsg" runat="server" Text="" ForeColor="blue"></asp:Label>
3 <asp:Button ID="btn" runat="server" Text="上传" Width="68px" OnClick="btn_Click"/>

4.编写按钮点击事件

我们这里将本地制作好的一个专题压缩成zip文件,上传到服务器上,在对文件进行解压,并删除原来的压缩文件。(确保zip中包好一个根目录文件夹

1 protected void btn_Click(object sender, EventArgs e)
 2     {
 3         //获取上传文件名 demo.zip
 4         string fileName = upZip.FileName;
 5         if (fileName == null || fileName=="")
 6         {
 7             lblMsg.Text = "没有选择文件";
 8         }
 9         else
10         {
11             //截取专题目录名 demo
12             string dirName = fileName.Substring(0, fileName.IndexOf('.'));
13             //获取上传目录 ~/zhuanti/2013/
14             string updir = "~/zhuanti/" + DateTime.Now.Year + "/";
15             //获取专题目录 ~/zhuanti/2013/demo/
16             string ztdir = updir + dirName +"/";
17             //转换为物理路径 E:\\root\\UI\\zhuanti\\2013\\demo\\
18             string abZtdir = Server.MapPath(ztdir);
19             //判断目录是否已经存在
20             if (Directory.Exists(abZtdir))
21             {//存在
22                 lblMsg.Text = "专题目录已存在";
23             }
24             else
25             {//不存在
26                 //判断压缩包类型
27                 string lastName = fileName.Substring(fileName.LastIndexOf("."));
28                 if (lastName.ToLower() == ".zip")
29                 {   
30                     //上传压缩包完整路径 ~/zhuanti/2013/demo.zip
31                     string fullpath = updir + fileName;
32                     //物理路径 E:\\root\\UI\\zhuanti\\2013\\demo.zip
33                     string abFullPath = Server.MapPath(fullpath);
34                     try
35                     {
36                         //上传目录是否存在
37                         if (!Directory.Exists(Server.MapPath(updir)))
38                         {
39                             Directory.CreateDirectory(Server.MapPath(updir));
40                         }
41                         //上传
42                         this.upZip.SaveAs(Server.MapPath(fullpath));
43                         //解压
44                         ZipUtil.UnpackFiles(abFullPath, Server.MapPath(updir));
45                         //删除压缩包
46                         if (File.Exists(abFullPath))
47                         {
48                             File.Delete(abFullPath);
49                         }
57                         loadFile();
58                     }
59                     catch (Exception ex)
60                     {
61                         lblMsg.Text = "操作失败";
62                     }
63 
64                 }
65                 else
66                 {
67                     lblMsg.Text = "只能上传ZIP文件";
68                 }
69             }
70         }
71         
72     }
View Code

 5.编写loadFile()方法,查看文件夹是否上传成功。这里用一个下拉列表控件显示目录下的所有文件夹

HTML代码

1 <asp:ListBox ID="lbxFile" runat="server" CausesValidation="True" Rows="20" SelectionMode="Multiple" Width="300px"></asp:ListBox>

CS文件代码

 1 //读取目录文件列表
 2     public void loadFile()
 3     {
      //要读取的目录物理路径
 4         string abdir = Server.MapPath("~/zhuanti/"+DateTime.Now.Year+"/");
      //创建DirectoryInfo对象
 5         DirectoryInfo theDir = new DirectoryInfo(abdir);
      //获取目录下所有子目录
 6         DirectoryInfo[] thisOne = theDir.GetDirectories();
      //获取目录下所有子目录(带路径)
 7         //string[] dirs = Directory.GetDirectories(abdir);
      //下拉框绑定数据
 8         lbxFile.DataSource = thisOne;
 9         lbxFile.DataBind();
10     }
View Code

 

 

posted @ 2013-11-08 08:28  思如雨  阅读(1007)  评论(0编辑  收藏  举报