Search code examples
asp.netcontent-typedownloadmime-types

ASP.NET Create zip file for download: the compressed zipped folder is invalid or corrupted


string fileName = "test.zip";
string path = "c:\\temp\\";
string fullPath = path + fileName;
FileInfo file = new FileInfo(fullPath);

Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.Buffer = true;
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.AppendHeader("content-length", file.Length.ToString());
Response.ContentType = "application/x-compressed";
Response.TransmitFile(fullPath);
Response.Flush();
Response.End();

The actual zip file c:\temp\test.zip is good, valid, whatever you want to call it. When I navigate to the directory c:\temp\ and double-click on the test.zip file; it opens right up.

My problem seems only to be with the download. The code above executes without any issue. A file download dialog is presented. I can chose 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.

For Response.ContentType I've tried:

application/x-compressed application/x-zip-compressed application/x-gzip-compresse application/octet-stream application/zip

The zip file is being created with some prior code (that I'm sure is working fine due to my ability to open the created file directly) using: Ionic.zip

http://www.codeplex.com/DotNetZip


Solution

  • This worked. I don't know why but it did.

    string fileName = "test.zip";
    string path = "c:\\temp\\";
    string fullPath = path + fileName;
    FileInfo file = new FileInfo(fullPath);
    
    Response.Clear();
    //Response.ClearContent();
    //Response.ClearHeaders();
    //Response.Buffer = true;
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
    //Response.AppendHeader("Content-Cength", file.Length.ToString());
    Response.ContentType = "application/x-zip-compressed";
    Response.WriteFile(fullPath);
    //Response.Flush();
    Response.End();