ASP.NET Create zip file for download: the compressed zipped

Jul 5 2019 6:46 AM
My problem seems only to be with the download. The code above executes without any issue. A file download dialog is presented. I can choose to either save or open. If I try to open the file from the dialog, or save it and then open it. I get the following dialog message:
 
The Compressed (zipped) Folder is invalid or corrupted.
 
my code
  1. List<string> folderpath = new List<string>();  
  2. var zip = new ZipFile();  
  3. gridview.AllowPaging = false;  
  4. Response.ClearContent();  
  5. string zipName = String.Format("Zip_{0}.zip", DateTime.Now.ToString("yyyy-MMM-dd-HHmmss"));  
  6. Response.AddHeader("content-disposition""attachment; filename=" + zipName);  
  7. Response.ContentType = "application/zip";  
  8. System.IO.StringWriter sw = new System.IO.StringWriter();  
  9. HtmlTextWriter htw = new HtmlTextWriter(sw);  
  10. gridview.RenderControl(htw);  
  11. //byte[] toBytes = Encoding.ASCII.GetBytes(zipName);  
  12. MemoryStream stream = new MemoryStream();  
  13. string attachment = sw.ToString();  
  14. byte[] data = Encoding.ASCII.GetBytes(attachment);  
  15. stream.Write(data, 0, data.Length);  
  16. stream.Seek(0, SeekOrigin.Begin);  

Answers (1)