Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate a temporary Zip file, then auto-remove it after it is downloaded?

Tags:

c#

asp.net

zip

I have a download page where there are 3 download options: Word, Zip, and PDF. There is a folder containing .doc files. When a user clicks the Zip option on the page, I want ASP.NET to zip the folder with the .doc files into a temporary .zip file. Then the client will download it from the server. When the user's download is finished, the temporary Zip file should delete itself.

How can I do this with ASP.NET 2.0 C#?

Note: I know how I can zip and unzip files and remove files from the system with C# ASP.NET 2.0.

like image 961
Ibrahim AKGUN Avatar asked May 28 '09 15:05

Ibrahim AKGUN


People also ask

Can you delete a zip file after you extract it?

To delete the compressed version, right-click the zipped folder > Choose [Delete].

Where do zip files go after download?

Zip files are usually downloaded to a location that is specific to your operating system. For Windows-based computers, zip files are typically saved in the My Documents or Downloads folder unless you've specified something else as the default download destination for your computer.

What do you do after you download a zip file?

After the download is complete, navigate to the ZIP file on your computer. You should be able to right-click or (if you are using Mac OS X) control-click on the file. At least one of the following options should appear in the menu: Extract, Extract All, Unzip.


1 Answers

Using DotNetZip you can save the zip file directly to the Response.OutputStream. No need for a temporary Zip file.

    Response.Clear();
    // no buffering - allows large zip files to download as they are zipped
    Response.BufferOutput = false;
    String ReadmeText= "Dynamic content for a readme file...\n" + 
                       DateTime.Now.ToString("G");
    string archiveName= String.Format("archive-{0}.zip", 
                                      DateTime.Now.ToString("yyyy-MMM-dd-HHmmss")); 
    Response.ContentType = "application/zip";
    Response.AddHeader("content-disposition", "attachment; filename=" + archiveName);
    using (ZipFile zip = new ZipFile())
    {
        // add a file entry into the zip, using content from a string
        zip.AddFileFromString("Readme.txt", "", ReadmeText);
        // add the set of files to the zip
        zip.AddFiles(filesToInclude, "files");
        // compress and write the output to OutputStream
        zip.Save(Response.OutputStream);
    }
    Response.Flush();
like image 182
Cheeso Avatar answered Oct 02 '22 10:10

Cheeso