code_junkie

Zipping Files and Downloading them Using SaveAs Dialog

Nov 13th, 2011
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.91 KB | None | 0 0
  1. protected void DownloadSelectedFiles_Click(object sender, EventArgs e)
  2. {
  3. string path = String.Empty;
  4. string zipFilePath = @"C:MyZipFile.zip";
  5.  
  6. ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath));
  7. byte[] buffer = new byte[4096];
  8.  
  9. foreach (GridViewRow row in gvFiles.Rows)
  10. {
  11. bool isSelected = (row.FindControl("chkSelect") as CheckBox).Checked;
  12.  
  13. if (isSelected)
  14. {
  15. path = (row.FindControl("lblUrl") as Label).Text;
  16.  
  17. ZipEntry entry = new ZipEntry(Path.GetFileName(path));
  18. entry.DateTime = DateTime.Now;
  19.  
  20. zipStream.PutNextEntry(entry);
  21.  
  22. using (FileStream fs = File.OpenRead(path))
  23. {
  24. int sourceBytes;
  25. do
  26. {
  27. sourceBytes = fs.Read(buffer, 0, buffer.Length);
  28. zipStream.Write(buffer, 0, sourceBytes);
  29. } while (sourceBytes > 0);
  30. }
  31. }
  32. }
  33.  
  34.  
  35. zipStream.Finish();
  36. zipStream.Close();
  37.  
  38. Response.ContentType = "application/x-zip-compressed";
  39. Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");
  40. Response.WriteFile(zipFilePath);
  41. Response.End();
  42.  
  43. }
  44.  
  45. Response.Buffer = true;
  46. Response.Clear();
  47. Response.ClearContent();
  48. Response.ClearHeaders();
  49.  
  50. Response.Flush()
  51. Response.Close()
  52.  
  53. Response.Buffer = true;
  54. Response.Clear();
  55. Response.ClearContent();
  56. Response.ClearHeaders();
  57. Response.ContentType = "application/x-zip-compressed";
  58. Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");
  59. Response.WriteFile(zipFilePath);
  60. Response.Flush()
  61. Response.Close()
  62. Response.End();
Add Comment
Please, Sign In to add comment