[ASP.NET]利用 DotNetZip 或 SharpZip 一次下載多個檔案

[ASP.NET]利用 DotNetZip 或 SharpZip 一次下載多個檔案

利用 DotNetZip or SharpZip 這兩個壓縮的Lib 可以將檔案壓縮起來

就像RAR那樣可以打包多個檔案為一個壓縮檔

這樣的功能,在web就可以達到一次下載多個檔案了

小弟就用一個實例來說明如何達到此功能

首先準備好要使用的DotNetZip 和 SharpZip dll,到下列網址下載

DotNetZip Url:
http://www.codeplex.com/DotNetZip

SharpZip Url:
http://www.icsharpcode.net/OpenSource/SharpZipLib/

此範例主要是先列出目錄底下所有檔案的清單,然後

1.可針對單一檔案下載(點HyperLink)

2.可勾選多個檔案,使用DotNetZip 或 SharpZip 壓縮為一個檔案輸出下載

MultiFileDownload.aspx

01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileDownload.aspx.cs"
02     Inherits="MultiFileDownload" %>
03   
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head runat="server">
07     <title>MultiFileDownload</title>
08 </head>
09 <body>
10     <form id="form1" runat="server">
11     <div>
12         <asp:Button ID="btnDotNetZip" runat="server" Text="利用DotNetZip下載" Width="200px" OnClick="btnDotNetZip_Click" />
13         <br />
14         <asp:Button ID="btnSharpZip" runat="server" Text="利用SharpZip下載" Width="200px" OnClick="btnSharpZip_Click" />
15         <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="200px">
16             <Columns>
17                 <asp:TemplateField HeaderText="Selected">
18                     <ItemTemplate>
19                         <asp:CheckBox ID="CheckBox1" runat="server" />
20                     </ItemTemplate>
21                     <ItemStyle HorizontalAlign="Center" />
22                 </asp:TemplateField>
23                 <asp:TemplateField HeaderText="File Name">
24                     <ItemTemplate>
25                         <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "zip/"+Eval("Name") %>'
26                             Text='<%# Eval("Name") %>'></asp:HyperLink>
27                     </ItemTemplate>
28                 </asp:TemplateField>
29             </Columns>
30         </asp:GridView>
31     </div>
32     </form>
33 </body>
34 </html>

 

MultiFileDownload.aspx.cs

001 using System;
002 using System.Collections.Generic;
003 using System.Web;
004 using System.Web.UI;
005 using System.Web.UI.WebControls;
006 using System.IO;
007   
008 //DotNetZip,加入Ionic.Zip.Partial.dll
009 using DotNetZip = Ionic.Zip;
010   
011 //SharpZip,加入ICSharpCode.SharpZipLib.dll
012 using SharpZip = ICSharpCode.SharpZipLib.Zip;
013   
014 public partial class MultiFileDownload : System.Web.UI.Page
015 {
016     protected void Page_Load(object sender, EventArgs e)
017     {
018         if (!IsPostBack)
019         {
020             this.GridView1.DataSource = GetFiles();
021             this.GridView1.DataBind();
022         }
023     }
024   
025     private List<FileInfo> GetFiles()
026     {
027         List<FileInfo> lstFileInfo = new List<FileInfo>();
028         string dir = Server.MapPath("zip");
029         FileInfo info;
030         string[] files;
031         files = Directory.GetFiles(dir);
032         foreach (string item in files)
033         {
034             info = new FileInfo(item);
035             lstFileInfo.Add(info);
036         }
037         return lstFileInfo;
038     }
039   
040     private List<FileInfo> GetSelectFile()
041     {
042         List<FileInfo> lstSelected = new List<FileInfo>();
043         foreach (GridViewRow item in this.GridView1.Rows)
044         {
045             //找出勾選的檔案
046             if (((CheckBox)item.Cells[0].Controls[1]).Checked)
047             {
048                 //記錄勾選的檔案
049                 lstSelected.Add(GetFiles().Find(delegate(FileInfo f) { return f.Name == ((HyperLink)item.Cells[1].Controls[1]).Text; }));
050             }
051         }
052         return lstSelected;
053     }
054   
055     #region DotNetZip
056   
057     //參考網址:http://blog.miniasp.com/?tag=/dotnetzip
058     protected void btnDotNetZip_Click(object sender, EventArgs e)
059     {
060         //利用DotNetZip壓縮輸出檔案
061   
062         List<FileInfo> lstSelected = GetSelectFile();
063         if (lstSelected.Count > 0)
064         {
065             Response.Clear();
066             Response.ContentType = "application/zip";
067             Response.AddHeader("content-disposition", "filename=" + "DotNetZip.zip");
068             using (DotNetZip.ZipFile zip = new DotNetZip.ZipFile(System.Text.Encoding.Default))//解決中文亂碼問題
069             {
070                 foreach (FileInfo file in lstSelected)
071                 {
072                     zip.AddFile(file.FullName, "");
073                 }
074                 zip.Save(Response.OutputStream);
075             }
076             Response.End();
077         }
078     }
079   
080     #endregion
081   
082     #region SharpZip
083   
085     protected void btnSharpZip_Click(object sender, EventArgs e)
086     {
087         //利用SharpZip壓縮輸出檔案
088   
089         List<FileInfo> lstSelected = GetSelectFile();
090         if (lstSelected.Count > 0)
091         {
092             Response.Clear();
093             Response.ContentType = "application/zip";
094             Response.AddHeader("content-disposition", "filename=" + "SharpZip.zip");
095             using (SharpZip.ZipOutputStream zip = new SharpZip.ZipOutputStream(Response.OutputStream))
096             {
097                 foreach (FileInfo file in lstSelected)
098                 {
099                     AddZipEntry(zip, "", file.FullName);
100                 }
101                 zip.Finish();
102                 zip.IsStreamOwner = false;// 避免 stream 被 zip 物件關閉
103             }
104             Response.End();
105         }
106     }
107   
109     private static void AddZipEntry(SharpZip.ZipOutputStream zip, string folder, string filename)
110     {
111         if (System.IO.Directory.Exists(filename) == true)
112         {
113             // Folder
114             System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(filename);
115             string folder_ = folder + di.Name + "/";
116             SharpZip.ZipEntry zFolder = new SharpZip.ZipEntry(folder_);
117             zip.PutNextEntry(zFolder);
118             foreach (System.IO.DirectoryInfo d in di.GetDirectories())
119                 AddZipEntry(zip, folder_, d.FullName);
120             foreach (System.IO.FileInfo f in di.GetFiles())
121                 AddZipEntry(zip, folder_, f.FullName);
122         }
123         else if (System.IO.File.Exists(filename) == true)
124         {
125             // File
126             zip.SetLevel(9);
127             using (System.IO.FileStream fs = System.IO.File.OpenRead(filename))
128             {
129                 byte[] b = new byte[fs.Length];
130                 fs.Read(b, 0, b.Length);
131                 SharpZip.ZipEntry z = new SharpZip.ZipEntry(folder + new System.IO.FileInfo(filename).Name);
132                 zip.PutNextEntry(z);
133                 zip.Write(b, 0, b.Length);
134             }
135         }
136     }
137   
138     #endregion
139 }

 

執行結果:


參考網址:

http://blog.miniasp.com/?tag=/dotnetzip
http://cheeso.members.winisp.net/DotNetZipHelp/Examples.htm
http://www.dotblogs.com.tw/chhuang/archive/2009/02/10/7102.aspx
http://www.dotblogs.com.tw/fatty0860/archive/2009/02/09/7084.aspx
http://richielin-programer.blogspot.com/2008/04/c-sharpzip.html
http://blog.csdn.net/cityhunter172/archive/2005/01/25/267150.aspx

posted @ 2010-08-27 09:23  peterlee  阅读(776)  评论(0)    收藏  举报