本文主要是介绍C#循环下载多个文件(把多个文件压缩成一个文件可以一次性下载)ICSharpCode.SharpZipLib.Zip 批量下载 内存存储不占硬盘,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
使用插件ICSharpCode.SharpZipLib.Zip
https://download.csdn.net/download/sunwork888/16038504
public void BatchQueryPolicyDownloadUrlZip(System.Web.HttpResponseBase Response, List<Ins> oList){using (System.IO.MemoryStream stream = new System.IO.MemoryStream()){using (ZipOutputStream zipStream = new ZipOutputStream(stream))//ZipOutputStream zipStream = new ZipOutputStream(File.Create("F:\\111\\111.zip"));{zipStream.SetLevel(6); //压缩级别0-9 ICSharpCode.SharpZipLib.Checksums.Crc32 crc = new ICSharpCode.SharpZipLib.Checksums.Crc32();foreach (var item in oList){var fileName = item.PolicyNo + item.InsuredName;var fileURL = item.PolicyDownloadUrl;System.Net.WebClient wc = new System.Net.WebClient();using (System.IO.MemoryStream streamUrl = new System.IO.MemoryStream(wc.DownloadData(fileURL))){byte[] buffer = streamUrl.ToArray();//建立压缩实体ZipEntry entry = new ZipEntry(fileName);//原文件名entry.DateTime = DateTime.Now;//空间大小crc.Reset();crc.Update(buffer);entry.Crc = crc.Value;zipStream.PutNextEntry(entry);zipStream.Write(buffer, 0, buffer.Length); }}zipStream.Finish();stream.Position = 0;Response.Buffer = true;Response.AddHeader("Content-Disposition", $"attachment; filename=附件_{DateTime.Now.ToString("yyMMddHHmmssfff")}.zip");Response.ContentType = "application/octet-stream";Response.BinaryWrite(stream.ToArray());zipStream.Close();stream.Close();GC.Collect();GC.Collect(1);}}}
这篇关于C#循环下载多个文件(把多个文件压缩成一个文件可以一次性下载)ICSharpCode.SharpZipLib.Zip 批量下载 内存存储不占硬盘的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!