这个多个文件下载生成zip格式必须先引用一个ICSharpCode.SharpZipLib.dll。
代码如下
//将多个文件打包成压缩文件zip格式下载
protected void PkgDownload(IEnumerable<string> files, string zipFileName)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;
using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();
file.NameTransform = new FileNameTransfom();
foreach (var item in files)
{
file.Add(item);
}
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length);
}
Response.AddHeader("content-disposition", "attachment;filename=" + zipFileName);
Response.BinaryWrite(buffer);
Response.Flush();
Response.End();
}
需要实现一个类
public class FileNameTransfom : ICSharpCode.SharpZipLib.Core.INameTransform
{
public string TransformDirectory(string name)
{
return null;
}
public string TransformFile(string name)
{
return Regex.Replace(Path.GetFileName(name), @"\d{17}_", "");
}
}
特别值得注意的是 这里得到的路径是绝对路径