利用 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" %> |
07 |
<title>MultiFileDownload</title> |
10 |
<form id="form1" runat="server"> |
12 |
<asp:Button ID="btnDotNetZip" runat="server" Text="利用DotNetZip下載" Width="200px" OnClick="btnDotNetZip_Click" /> |
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"> |
17 |
<asp:TemplateField HeaderText="Selected"> |
19 |
<asp:CheckBox ID="CheckBox1" runat="server" /> |
21 |
<ItemStyle HorizontalAlign="Center" /> |
23 |
<asp:TemplateField HeaderText="File Name"> |
25 |
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "zip/"+Eval("Name") %>' |
26 |
Text='<%# Eval("Name") %>'></asp:HyperLink> |
MultiFileDownload.aspx.cs
002 |
using System.Collections.Generic; |
005 |
using System.Web.UI.WebControls; |
009 |
using DotNetZip = Ionic.Zip; |
012 |
using SharpZip = ICSharpCode.SharpZipLib.Zip; |
014 |
public partial class MultiFileDownload : System.Web.UI.Page |
016 |
protected void Page_Load(object sender, EventArgs e) |
020 |
this.GridView1.DataSource = GetFiles(); |
021 |
this.GridView1.DataBind(); |
025 |
private List<FileInfo> GetFiles() |
027 |
List<FileInfo> lstFileInfo = new List<FileInfo>(); |
028 |
string dir = Server.MapPath("zip"); |
031 |
files = Directory.GetFiles(dir); |
032 |
foreach (string item in files) |
034 |
info = new FileInfo(item); |
035 |
lstFileInfo.Add(info); |
040 |
private List<FileInfo> GetSelectFile() |
042 |
List<FileInfo> lstSelected = new List<FileInfo>(); |
043 |
foreach (GridViewRow item in this.GridView1.Rows) |
046 |
if (((CheckBox)item.Cells[0].Controls[1]).Checked) |
049 |
lstSelected.Add(GetFiles().Find(delegate(FileInfo f) { return f.Name == ((HyperLink)item.Cells[1].Controls[1]).Text; })); |
058 |
protected void btnDotNetZip_Click(object sender, EventArgs e) |
062 |
List<FileInfo> lstSelected = GetSelectFile(); |
063 |
if (lstSelected.Count > 0) |
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)) |
070 |
foreach (FileInfo file in lstSelected) |
072 |
zip.AddFile(file.FullName, ""); |
074 |
zip.Save(Response.OutputStream); |
085 |
protected void btnSharpZip_Click(object sender, EventArgs e) |
089 |
List<FileInfo> lstSelected = GetSelectFile(); |
090 |
if (lstSelected.Count > 0) |
093 |
Response.ContentType = "application/zip"; |
094 |
Response.AddHeader("content-disposition", "filename=" + "SharpZip.zip"); |
095 |
using (SharpZip.ZipOutputStream zip = new SharpZip.ZipOutputStream(Response.OutputStream)) |
097 |
foreach (FileInfo file in lstSelected) |
099 |
AddZipEntry(zip, "", file.FullName); |
102 |
zip.IsStreamOwner = false; |
109 |
private static void AddZipEntry(SharpZip.ZipOutputStream zip, string folder, string filename) |
111 |
if (System.IO.Directory.Exists(filename) == true) |
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); |
123 |
else if (System.IO.File.Exists(filename) == true) |
127 |
using (System.IO.FileStream fs = System.IO.File.OpenRead(filename)) |
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); |
133 |
zip.Write(b, 0, b.Length); |
執行結果:

參考網址:
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