public class MyNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform { #region INameTransform 成員 public string TransformDirectory(string name) { return null; } public string TransformFile(string name) { return Path.GetFileName(name); } #endregion } public partial class Default1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { ZipFileByCode(); } /// <summary> /// 壓縮打包文件 /// </summary> public void ZipFileByCode() { MemoryStream ms = new MemoryStream(); byte[] buffer = null; using (ZipFile file = ZipFile.Create(ms)) { file.BeginUpdate(); file.NameTransform = new MyNameTransfom();//經過這個名稱格式化器,能夠將裏面的文件名進行一些處理。默認狀況下,會自動根據文件的路徑在zip中建立有關的文件夾。 file.Add(Server.MapPath("/Content/images/img01.jpg")); file.Add(Server.MapPath("/Content/images/img02.jpg")); file.CommitUpdate(); buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); } Response.AddHeader("content-disposition", "attachment;filename=test.zip"); Response.BinaryWrite(buffer); Response.Flush(); Response.End(); } }
所需Dll下載:http://files.cnblogs.com/wangbogo/ICSharpCode.SharpZipLib.rar spa