Jason Braswell
Jason Braswell

Reputation: 399

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

Upvotes: 6

Views: 34412

Answers (2)

Roberto
Roberto

Reputation: 1

When downloading to client I like to use this method. It´ll allow the client to choose the path in which they want the file to be downloaded (be aware that some browsers doesn't allow this):

System.IO.FileInfo file =
    new System.IO.FileInfo(filePath); // full file path, i.e: from disk to file
Response.ClearContent(); // just in case we remove (if any) written content
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.AddHeader("Content-Length", file.Length.ToString());
Response.ContentType = "application/x-zip-compressed"; // compressed file type
Response.TransmitFile(file.FullName); // TransmitFile allows client to choose folder
Response.End();

Upvotes: 0

Jason Braswell
Jason Braswell

Reputation: 399

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();

Upvotes: 24

Related Questions