1.多byte[] 打包下载
var streamsDict = new Dictionary<string, byte[]>(); // 循环添加到 streamsDict var ds=new DataSet(); streamsDict.Add(ds.DataSetName+ ".xls", NPOIHelper.ExportDataSet(ds).GetBuffer()); HttpContext curContext = HttpContext.Current; MemoryStream ms = new MemoryStream(); ms = DownLoadHelper.PackageZip(streamsDict); byte[] bytes = new byte[(int)ms.Length]; ms.Read(bytes, 0, bytes.Length); ms.Close(); curContext.Response.ContentType = "application/octet-stream"; curContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode($"数据下载{DateTime.Now:yyyyMMddHHmmss}.zip", System.Text.Encoding.UTF8)); curContext.Response.BinaryWrite(bytes); curContext.Response.Flush();
2.多文件打包下载
// existPathlist list,文件路径 if (existPathlist.Any()) { var buffer = DownLoadHelper.Download(existPathlist, $"评级信息{DateTime.Now:yyyyMMddHHmmss}.zip"); Response.AddHeader("content-disposition", "attachment;filename=" + $"评级信息{DateTime.Now:yyyyMMddHHmmss}.zip"); Response.BinaryWrite(buffer); Response.Flush(); Response.End(); }
public static class DownLoadHelper { public static byte[] Download(IEnumerable<string> files, string zipFileName) { //根据所选文件打包下载 MemoryStream ms = new MemoryStream(); byte[] buffer = null; using (ZipFile file = ZipFile.Create(ms)) { file.BeginUpdate(); file.NameTransform = new MyNameTransfom();//通过这个名称格式化器,可以将里面的文件名进行一些处理。默认情况下,会自动根据文件的路径在zip中创建有关的文件夹。 foreach (var item in files) { file.Add(item); } file.CommitUpdate(); buffer = new byte[ms.Length]; ms.Position = 0; ms.Read(buffer, 0, buffer.Length); } return buffer; } /// <summary> /// 将多个流进行zip压缩,返回压缩后的流 /// </summary> /// <param name="streams">key:文件名;value:文件名对应的要压缩的流</param> /// <returns>压缩后的流</returns> public static MemoryStream PackageZip(Dictionary<string, Stream> streams) { byte[] buffer = new byte[6500]; MemoryStream returnStream = new MemoryStream(); var zipMs = new MemoryStream(); using (ZipOutputStream zipStream = new ZipOutputStream(zipMs)) { zipStream.SetLevel(9); foreach (var kv in streams) { string fileName = kv.Key; using (var streamInput = kv.Value) { zipStream.PutNextEntry(new ZipEntry(fileName)); while (true) { var readCount = streamInput.Read(buffer, 0, buffer.Length); if (readCount > 0) { zipStream.Write(buffer, 0, readCount); } else { break; } } zipStream.Flush(); } } zipStream.Finish(); zipMs.Position = 0; zipMs.CopyTo(returnStream, 5600); } returnStream.Position = 0; return returnStream; } /// <summary> /// 将多个byte[]进行zip压缩,返回压缩后的流 /// </summary> /// <param name="streams">key:文件名;value:文件名对应的byte</param> /// <returns>压缩后的流</returns> public static MemoryStream PackageZip(Dictionary<string, byte[]> streams) { MemoryStream returnStream = new MemoryStream(); var zipMs = new MemoryStream(); using (ZipOutputStream zipStream = new ZipOutputStream(zipMs)) { zipStream.SetLevel(9); foreach (var kv in streams) { string fileName = kv.Key; var streamInput = kv.Value; zipStream.PutNextEntry(new ZipEntry(fileName)); zipStream.Write(streamInput, 0, streamInput.Length); zipStream.Flush(); } zipStream.Finish(); zipMs.Position = 0; zipMs.CopyTo(returnStream, 5600); } returnStream.Position = 0; return returnStream; } } 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 }