5

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.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
İbrahim Akgün
  • 1,527
  • 11
  • 37
  • 63

4 Answers4

9

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();
Cheeso
  • 189,189
  • 101
  • 473
  • 713
  • as I said in the post, that code uses DotNetZip - available at http://dotnetzip.codeplex.com. If that's not what you mean, i don't know what you are asking. – Cheeso Oct 10 '11 at 12:58
  • 1
    i downloaded the code from your link then there were two dlls, Ionic.zip.dll and Ionic.ziplib.dll, that i was asking which dll. But I test both and found Ionic.Zip.dll for my requirement task. Thankx for your valuable post. – Siddiqui Oct 11 '11 at 07:04
  • 1
    Instead of Response.Close() use Response.End() – rball Oct 02 '12 at 17:29
  • 1
    @rball is incorrect. Instead of `Response.Close();` use `Response.Flush();` See https://stackoverflow.com/a/736462/481207 – Matt Nov 12 '17 at 21:31
  • the answer has been edited to call `Response.Flush()` – Cheeso Mar 07 '18 at 00:11
  • Since DotNetZip is now moved to the CodePlex archive https://archive.codeplex.com/?p=dotnetzip and I could not find a binary within the download on that site, I thought I would mention that you can get a forked version of DotZetZip out on Nuget. Within VS.Net, simply right click your project then click Mange Nuget packages, then search for "DotNetZip" and click Install. – Jeff Mergler Apr 11 '18 at 21:31
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();
                }
            }
Amir
  • 21
  • 2
0

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" );
}
Paul Alexander
  • 31,970
  • 14
  • 96
  • 151
  • while response.write in action client send 2 request Post and Get to server . in first action its enter try block bla bla bla than enter Finally block and remove file .. and fire event again and try to download file but there is tempziP file cuz its removed in first request... SOO How can i solve this problem ? – İbrahim Akgün May 28 '09 at 21:37
0

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)};
Jason Plank
  • 2,336
  • 5
  • 31
  • 40
İbrahim Akgün
  • 1,527
  • 11
  • 37
  • 63