C#中文件多选 批量下载

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


/// <summary>
/// 批量进行多个文件压缩到一个文件、
/// <param name="files">文件列表(绝对路径)</param> 这里用的数组,你可以用list 等或者
/// <param name="zipFileName">生成的zip文件名称</param>
/// </summary>
public void ZipFileDownload(string[] files, string zipFileName,HttpContext Context)
{
MemoryStream ms = new MemoryStream();
byte[] buffer = null;

using (ZipFile file = ZipFile.Create(ms))
{
file.BeginUpdate();

file.NameTransform = new MyNameTransfom();
foreach (var item in files)
{
if (File.Exists(Context.Server.MapPath(item)))
file.Add(Context.Server.MapPath(item));

}
file.CommitUpdate();
buffer = new byte[ms.Length];
ms.Position = 0;
ms.Read(buffer, 0, buffer.Length); //读取文件内容(1次读ms.Length/1024M)
ms.Flush();
ms.Close();
}
Context.Response.Clear();
Context.Response.Buffer = true;
Context.Response.ContentType = "application/x-zip-compressed";
Context.Response.AddHeader("content-disposition", "attachment;filename=" + HttpUtility.UrlEncode(zipFileName));
Context.Response.BinaryWrite(buffer);
Context.Response.Flush();
Context.Response.End();
}
}

 

 

 

//调用 也可以用集合

//string[] aa = { "/DownloadBuyPic/20161019073315220.zip", "/DownloadBuyPic/BigPic/20161020043805152.jpg" };
//mt.ZipFileDownload(aa, DateTime.Now.ToString("yyyyMMddhhMmss") + "_检品报告书.zip",HttpContext.Current);

转载于:https://www.cnblogs.com/liwp/p/5997623.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值