How can I create a temporary zip file and then automatically delete it after downloading it?

I have a download page where there are three download options: Word, Zip and PDF. There is a folder containing .doc files. When the user clicks the Zip button 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 download is complete, the temporary zip file should delete itself.

How to do this using ASP.NET 2.0 C #?

Note. I know how I can unzip and unzip files and delete files from the system using C # ASP.NET 2.0.

+4
c # zip
source share
4 answers

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)}; 
0
source share

Using DotNetZip , you can save the zip file directly to Response.OutputStream. No need for 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(); 
+7
source share

Loading a form from a database and zip code To use this code in a class, you must use 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(); } } 
+2
source share

You will want to manually download the zip file to the user, and then delete the file when streaming is complete.

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

All Articles