简体   繁体   English

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

[英]download multiple files as zip in .net

I have a program which needs to download multiple files at once.我有一个程序需要一次下载多个文件。 I can download a single file by using this single file download , but it doesn't work for multiple.我可以使用此单个文件下载来下载单个文件,但它不适用于多个文件。

How can one download multiple files at once in such as as zip file?如何一次下载多个文件,例如 zip 文件?

You need to pack files and write a result to a response.您需要打包文件并将结果写入响应。 You can use SharpZipLib compression library.您可以使用SharpZipLib压缩库。

Code example:代码示例:

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();
}

This is how to do it the DotNetZip way :DI vouch for DotNetZip because I have used it and it is by far the easiest compression library for C# I've come across :)这是如何通过 DotNetZip 方式做到这一点:我为 DotNetZip 提供担保,因为我已经使用过它,而且它是迄今为止我遇到的最简单的 C# 压缩库:)

Check http://dotnetzip.codeplex.com/检查http://dotnetzip.codeplex.com/

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

Create a downloadable zip within ASP.NET.在 ASP.NET 中创建可下载的 zip。 This example creates a zip dynamically within an ASP.NET postback method, then downloads that zipfile to the requesting browser through Response.OutputStream.此示例在 ASP.NET 回发方法中动态创建一个 zip,然后通过 Response.OutputStream 将该 zip 文件下载到请求浏览器。 No zip archive is ever created on disk.从来没有在磁盘上创建 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 文件。

The 3 libraries I know of are SharpZipLib (versatile formats), DotNetZip (everything ZIP), and ZipStorer (small and compact).我知道的 3 个库是 SharpZipLib(通用格式)、DotNetZip(所有 ZIP)和 ZipStorer(小巧紧凑)。 No links, but they are all on codeplex and found via google.没有链接,但它们都在 codeplex 上并通过谷歌找到。 The licenses and exact features vary.许可证和确切功能各不相同。

Happy coding.快乐编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM