在.net中以zip格式下载多个文件

我有一个文件列表,每个文件都有复选框,如果用户检查了很多文件并点击下载,我必须压缩所有这些文件并下载…就像在邮件附件中一样..

我已经使用这篇文章中提到的代码进行单个文件下载

请帮助如何下载多个文件作为zip ..

您需要打包文件并将结果写入响应。 您可以使用SharpZipLib压缩库。

代码示例:

Response.AddHeader("Content-Disposition", "attachment; filename=" + compressedFileName + ".zip"); Response.ContentType = "application/zip"; using (var zipStream = new ZipOutputStream(Response.OutputStream)) { foreach (string filePath in filePaths) { byte[] fileBytes = System.IO.File.ReadAllBytes(filePath); var fileEntry = new ZipEntry(Path.GetFileName(filePath)) { Size = fileBytes.Length }; zipStream.PutNextEntry(fileEntry); zipStream.Write(fileBytes, 0, fileBytes.Length); } zipStream.Flush(); zipStream.Close(); } 

这是如何使用DotNetZip的方式:因为我已经使用它而对DotNetZip的DI保证它是迄今为止最简单的C#压缩库我遇到过:)

查看http://dotnetzip.codeplex.com/

http://dotnetzip.codeplex.com/wikipage?title=CS-Examples&referringTitle=Examples

在ASP.NET中创建可下载的zip。 此示例在ASP.NET回发方法中动态创建zip,然后通过Response.OutputStream将该zipfile下载到请求的浏览器。 在磁盘上永远不会创建zip存档。

 public void btnGo_Click (Object sender, EventArgs e) { Response.Clear(); Response.BufferOutput= false; // for large files String ReadmeText= "This is a zip file dynamically generated at " + System.DateTime.Now.ToString("G"); string filename = System.IO.Path.GetFileName(ListOfFiles.SelectedItem.Text) + ".zip"; Response.ContentType = "application/zip"; Response.AddHeader("content-disposition", "filename=" + filename); using (ZipFile zip = new ZipFile()) { zip.AddFile(ListOfFiles.SelectedItem.Text, "files"); zip.AddEntry("Readme.txt", "", ReadmeText); zip.Save(Response.OutputStream); } Response.Close(); } 

使用http://www.icsharpcode.net/opensource/sharpziplib/动态创建ZIP文件。

我所知道的3个库是SharpZipLib(通用格式),DotNetZip(所有ZIP)和ZipStorer(小型和紧凑型)。 没有链接,但它们都在codeplex上,并通过谷歌找到。 许可证和确切function各不相同。

快乐的编码。