利用 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
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="MultiFileDownload.aspx.cs" Inherits="MultiFileDownload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>MultiFileDownload</title> </head> <body> <form id="form1" runat="server"> <div> <asp:Button ID="btnDotNetZip" runat="server" Text="利用DotNetZip下載" Width="200px" OnClick="btnDotNetZip_Click" /> <br /> <asp:Button ID="btnSharpZip" runat="server" Text="利用SharpZip下載" Width="200px" OnClick="btnSharpZip_Click" /> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="200px"> <Columns> <asp:TemplateField HeaderText="Selected"> <ItemTemplate> <asp:CheckBox ID="CheckBox1" runat="server" /> </ItemTemplate> <ItemStyle HorizontalAlign="Center" /> </asp:TemplateField> <asp:TemplateField HeaderText="File Name"> <ItemTemplate> <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# "zip/"+Eval("Name") %>' Text='<%# Eval("Name") %>'></asp:HyperLink> </ItemTemplate> </asp:TemplateField> </Columns> </asp:GridView> </div> </form> </body> </html>
MultiFileDownload.aspx.cs
using System; using System.Collections.Generic; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; //DotNetZip,加入Ionic.Zip.Partial.dll using DotNetZip = Ionic.Zip; //SharpZip,加入ICSharpCode.SharpZipLib.dll using SharpZip = ICSharpCode.SharpZipLib.Zip; public partial class MultiFileDownload : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { this.GridView1.DataSource = GetFiles(); this.GridView1.DataBind(); } } private List<FileInfo> GetFiles() { List<FileInfo> lstFileInfo = new List<FileInfo>(); string dir = Server.MapPath("zip"); FileInfo info; string[] files; files = Directory.GetFiles(dir); foreach (string item in files) { info = new FileInfo(item); lstFileInfo.Add(info); } return lstFileInfo; } private List<FileInfo> GetSelectFile() { List<FileInfo> lstSelected = new List<FileInfo>(); foreach (GridViewRow item in this.GridView1.Rows) { //找出勾選的檔案 if (((CheckBox)item.Cells[0].Controls[1]).Checked) { //記錄勾選的檔案 lstSelected.Add(GetFiles().Find(delegate(FileInfo f) { return f.Name == ((HyperLink)item.Cells[1].Controls[1]).Text; })); } } return lstSelected; } #region DotNetZip //參考網址:http://blog.miniasp.com/?tag=/dotnetzip protected void btnDotNetZip_Click(object sender, EventArgs e) { //利用DotNetZip壓縮輸出檔案 List<FileInfo> lstSelected = GetSelectFile(); if (lstSelected.Count > 0) { Response.Clear(); Response.ContentType = "application/zip"; Response.AddHeader("content-disposition", "filename=" + "DotNetZip.zip"); using (DotNetZip.ZipFile zip = new DotNetZip.ZipFile(System.Text.Encoding.Default))//解決中文亂碼問題 { foreach (FileInfo file in lstSelected) { zip.AddFile(file.FullName, ""); } zip.Save(Response.OutputStream); } Response.End(); } } #endregion #region SharpZip //參考網址:http://richielin-programer.blogspot.com/2008/04/c-sharpzip.html protected void btnSharpZip_Click(object sender, EventArgs e) { //利用SharpZip壓縮輸出檔案 List<FileInfo> lstSelected = GetSelectFile(); if (lstSelected.Count > 0) { Response.Clear(); Response.ContentType = "application/zip"; Response.AddHeader("content-disposition", "filename=" + "SharpZip.zip"); using (SharpZip.ZipOutputStream zip = new SharpZip.ZipOutputStream(Response.OutputStream)) { foreach (FileInfo file in lstSelected) { AddZipEntry(zip, "", file.FullName); } zip.Finish(); zip.IsStreamOwner = false;// 避免 stream 被 zip 物件關閉 } Response.End(); } } //參考網址:http://richielin-programer.blogspot.com/2008/04/c-sharpzip.html private static void AddZipEntry(SharpZip.ZipOutputStream zip, string folder, string filename) { if (System.IO.Directory.Exists(filename) == true) { // Folder System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(filename); string folder_ = folder + di.Name + "/"; SharpZip.ZipEntry zFolder = new SharpZip.ZipEntry(folder_); zip.PutNextEntry(zFolder); foreach (System.IO.DirectoryInfo d in di.GetDirectories()) AddZipEntry(zip, folder_, d.FullName); foreach (System.IO.FileInfo f in di.GetFiles()) AddZipEntry(zip, folder_, f.FullName); } else if (System.IO.File.Exists(filename) == true) { // File zip.SetLevel(9); using (System.IO.FileStream fs = System.IO.File.OpenRead(filename)) { byte[] b = new byte[fs.Length]; fs.Read(b, 0, b.Length); SharpZip.ZipEntry z = new SharpZip.ZipEntry(folder + new System.IO.FileInfo(filename).Name); zip.PutNextEntry(z); zip.Write(b, 0, b.Length); } } } #endregion }
執行結果:
參考網址:
http://blog.miniasp.com/?tag=/dotnetziphttp://cheeso.members.winisp.net/DotNetZipHelp/Examples.htmhttp://www.dotblogs.com.tw/chhuang/archive/2009/02/10/7102.aspxhttp://www.dotblogs.com.tw/fatty0860/archive/2009/02/09/7084.aspxhttp://richielin-programer.blogspot.com/2008/04/c-sharpzip.htmlhttp://blog.csdn.net/cityhunter172/archive/2005/01/25/267150.aspx
# re: [ASP.NET]利用 DotNetZip 或 SharpZip 一次下載多個檔案, Posted by bluemouse on 2009/7/11 上午 08:32 回覆
你好,看到你的文章写的很好,我最近也想在弄个压缩下载,麻烦你把DotNetZip.dll 和 SharpZip dll 邮件发给我一份,谢谢 我的邮件:33979657@qq.com
# re: [ASP.NET]利用 DotNetZip 或 SharpZip 一次下載多個檔案, Posted by puma on 2009/7/11 上午 10:49 回覆
to bluemouse,
mail 給你了..
# re: [ASP.NET]利用 DotNetZip 或 SharpZip 一次下載多個檔案, Posted by chjiang on 2009/10/1 上午 11:22 回覆
您好,可以和您要此範例檔與dll檔案嗎?謝謝您!!
# re: [ASP.NET]利用 DotNetZip 或 SharpZip 一次下載多個檔案, Posted by 小乐 on 2010/1/26 上午 11:31 回覆
嗯,不错,学习了。