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

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.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

Form Download From DataBase And Zip And Complate Download Remove
For Use this Code In Class need using ICSharpCode.SharpZipLib.Zip

 if (ds.Tables[0].Rows.Count > 0)
            {
                // Create the ZIP file that will be downloaded. Need to name the file something unique ...
                string strNow = String.Format("{0:MMM-dd-yyyy_hh-mm-ss}", System.DateTime.Now);
                ZipOutputStream zipOS = new ZipOutputStream(File.Create(Server.MapPath("~/TempFile/") + strNow + ".zip"));
                zipOS.SetLevel(5); // ranges 0 to 9 ... 0 = no compression : 9 = max compression

                // Loop through the dataset to fill the zip file
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    byte[] files = (byte[])(dr["Files"]);
                    //FileStream strim = new FileStream(Server.MapPath("~/TempFile/" + dr["FileName"]), FileMode.Create);
                    //strim.Write(files, 0, files.Length);
                    //strim.Close();
                    //strim.Dispose();
                    ZipEntry zipEntry = new ZipEntry(dr["FileName"].ToString());
                    zipOS.PutNextEntry(zipEntry);
                    zipOS.Write(files, 0, files.Length);
                }
                zipOS.Finish();
                zipOS.Close();

                FileInfo file = new FileInfo(Server.MapPath("~/TempFile/") + strNow + ".zip");
                if (file.Exists)
                {
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
                    Response.AddHeader("Content-Length", file.Length.ToString());
                    Response.ContentType = "application/zip";
                    Response.WriteFile(file.FullName);
                    Response.Flush();
                    file.Delete();
                    Response.End();
                }
            }

Method 3

You’ll want to stream the zip file to the user manually, then delete the file when streaming is complete.

try
{
    Response.WriteFile( "path to .zip" );
}
finally
{
    File.Delete( "path to .zip" );
}

Method 4

I fixed my problem by adding this to the end of the stream code:

Response.Flush();
Response.Close();
if(File.Exist(tempFile))
{File.Delete(tempFile)};


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x