11

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?

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
deepu
  • 1,993
  • 6
  • 42
  • 64

5 Answers5

30

You need to pack files and write a result to a response. You can use SharpZipLib compression library.

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();
}
bniwredyc
  • 8,649
  • 1
  • 39
  • 52
  • That is great...well deserving of an upvote. Do you know if there is any real time way to know the compressed size? I want to be able to tell my users the size of what they will download if compressed. – pearcewg Oct 06 '11 at 22:46
  • 1
    @pearcewg, I think in this case solution depends on your requirement. If you know what will be a content of archives you can compress files before page generation and just show archives' size. If content of the archives may vary then it's nontrivial task. My thoughts are: 1. put information about compressed files size in database 2. show approximate size of compressed files according to statistics of compression. – bniwredyc Oct 07 '11 at 06:01
  • @bniwredyc What is `foreach (string filePath in filePaths)`, I mean where did you fill list called filePaths, could you explain this code little bit? – Roxy'Pro Oct 22 '16 at 11:14
4

This is how to do it the DotNetZip way :D I vouch for DotNetZip because I have used it and it is by far the easiest compression library for C# I've come across :)

Check http://dotnetzip.codeplex.com/

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

Create a downloadable zip within ASP.NET. This example creates a zip dynamically within an ASP.NET postback method, then downloads that zipfile to the requesting browser through Response.OutputStream. No zip archive is ever created on disk.

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();
}
Ranhiru Jude Cooray
  • 19,542
  • 20
  • 83
  • 128
  • Hello.. Thanks for this Nice Reply.. But i want to know that whether i can change the name of the file before adding into zip(before zip.AddFile command). I am talking about the file which will going to be added into the ZIP not the file name of the zip file. – Sagar Rawal Sep 26 '13 at 11:42
1

Create a ZIP file on the fly using http://www.icsharpcode.net/opensource/sharpziplib/.

Pieter van Ginkel
  • 29,160
  • 8
  • 71
  • 111
1

The 3 libraries I know of are SharpZipLib (versatile formats), DotNetZip (everything ZIP), and ZipStorer (small and compact). No links, but they are all on codeplex and found via google. The licenses and exact features vary.

Happy coding.

0
var memoryStream = new MemoryStream();

using (var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, false))

{
    // Files means your file
    foreach (var file in files)

    {

        var file = _converter.Convert(pdf);

        var entry = zipArchive.CreateEntry($"{await GetUserName(userId)}.pdf");

        using (Stream str = entry.Open())

        {

            str.Write(file);

        }

    }

}

var outdate = memoryStream.ToArray();

return File(outdate, "application/zip", "Trainingcard.zip");
user16217248
  • 3,119
  • 19
  • 19
  • 37
alexa
  • 1
  • 1
  • A comment/some text to explain/support everything would be nice. But most importantly, stick to the formatting rules of stackoverflow – Z-100 Jun 01 '23 at 06:46