Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Download All Files as Zip

Tags:

c#

.net

asp.net

zip

I have a folder on my web server that has hundreds of mp3 files in it. I would like to provide the option for a user to download a zipped archive of every mp3 in the directory from a web page.

I want to compress the files programmatically only when needed. Because the zip file will be quite large, I am thinking that I will need to send the zip file to the response stream as it is being zipped, for performance reasons.

Is this possible? How can I do it?

like image 888
Ronnie Overby Avatar asked Apr 19 '10 19:04

Ronnie Overby


People also ask

How to zip a folder (directory) and download in ASP NET?

Zip a Folder (Directory) and Download in ASP.Net using C# and VB.Net. When the Download Button is clicked, first an object of the DotNetZip Library is created and a loop is executed over the GridView rows and the all the files for which the Checkbox is checked are added to the Zip object. Finally the Zip object is downloaded as Zip file.

How to download multiple files as a zipped file in Visual Studio?

Given below are the steps for downloading multiple files as a zipped file. Go to Visual Studio, open new ASP.NET Web application, and assign a relevant project name. Follow the below screenshot.

How to create a zip file in MVC programmatically?

The creation of the zip file and addition of the selected files can be done programmatically in an ASP.NET MVC Controller. Go to my own GitHub link to download this Project. Steps to be followed. Add the DLL files are in reference folder of the project.

How to zip a folder and download a folder in GridView?

Zip a Folder (Directory) and Download in ASP.Net using C# and VB.Net When the Download Button is clicked, first an object of the DotNetZip Library is created and a loop is executed over the GridView rows and the all the files for which the Checkbox is checked are added to the Zip object.


2 Answers

Here is code I use to do this with DotNetZip - works very well. Obviously you will need to provide the variables for outputFileName, folderName, and includeSubFolders.

response.ContentType = "application/zip";
response.AddHeader("content-disposition", "attachment; filename=" + outputFileName);
using (ZipFile zipfile = new ZipFile()) {
  zipfile.AddSelectedFiles("*.*", folderName, includeSubFolders);
  zipfile.Save(response.OutputStream);
}
like image 92
Ray Avatar answered Sep 28 '22 06:09

Ray


I can't believe how easy this was. After reading this, here is the code that I used:

protected void Page_Load(object sender, EventArgs e)
{
    Response.Clear();
    Response.BufferOutput = false;
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "attachment; filename=pauls_chapel_audio.zip");

    using (ZipFile zip = new ZipFile())
    {
        zip.CompressionLevel = CompressionLevel.None;
        zip.AddSelectedFiles("*.mp3", Server.MapPath("~/content/audio/"), "", false);
        zip.Save(Response.OutputStream);
    }

    Response.Close();
}
like image 32
Ronnie Overby Avatar answered Sep 28 '22 07:09

Ronnie Overby