1 public class IndexController: ICSharpCode.SharpZipLib.Core.INameTransform 2 { 3 4 #region INameTransform 成员 5 6 public string TransformDirectory(string name) 7 { 8 return null; 9 } 10 11 public string TransformFile(string name) 12 { 13 return Path.GetFileName(name); 14 } 15 16 #endregion 17 } 18 19 /// <summary> 20 /// 压缩打包文件 21 /// </summary> 22 public ActionResult ZipFileByCode() 23 { 24 MemoryStream ms = new MemoryStream(); 25 byte[] buffer = null; 26 27 using (ZipFile file = ZipFile.Create(ms)) 28 { 29 file.BeginUpdate(); 30 file.NameTransform = new IndexController();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。 31 32 file.Add(Server.MapPath("/Content/images/img01.jpg")); 33 file.Add(Server.MapPath("/Content/images/img02.jpg")); 34 file.CommitUpdate(); 35 36 buffer = new byte[ms.Length]; 37 ms.Position = 0; 38 ms.Read(buffer, 0, buffer.Length); 39 } 40 Response.AddHeader("content-disposition", "attachment;filename=test.zip"); 41 Response.BinaryWrite(buffer); 42 Response.Flush(); 43 Response.End(); 44 45 return new EmptyResult(); 46 } 47 48 }
所需Dll下载:http://files.cnblogs.com/wangbogo/ICSharpCode.SharpZipLib.rar