tags:

views:

269

answers:

5

I have the following code to zip all the files and then save it to the harddisk. I want zip all the files (this is done) and then attach the zip file to the Response stream so that the user have the option to save it!

protected void DownloadSelectedFiles_Click(object sender, EventArgs e)
        {
            string path = String.Empty;
            string zipFilePath = @"C:\MyZipFile.zip";

            ZipOutputStream zipStream = new ZipOutputStream(File.Create(zipFilePath));
            byte[] buffer = new byte[4096];

            foreach (GridViewRow row in gvFiles.Rows)
            {
                bool isSelected = (row.FindControl("chkSelect") as CheckBox).Checked;

                if (isSelected)
                {
                    path = (row.FindControl("lblUrl") as Label).Text;

                    ZipEntry entry = new ZipEntry(Path.GetFileName(path));
                    entry.DateTime = DateTime.Now;

                    zipStream.PutNextEntry(entry);

                    using (FileStream fs = File.OpenRead(path))
                    {
                        int sourceBytes;
                        do
                        {
                            sourceBytes = fs.Read(buffer, 0, buffer.Length);
                            zipStream.Write(buffer, 0, sourceBytes);
                        } while (sourceBytes > 0);
                    }
                }
            }


            zipStream.Finish();
            zipStream.Close();

            Response.ContentType = "application/x-zip-compressed";
            Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");
            Response.WriteFile(zipFilePath);
            Response.End();

        }
+1  A: 

If you are using IE, check that its not the old "cache is full"-bug that is showing its ugly face.

And if you have IE set to refresh cashe Automatically or on IE-start and have downloaded that zip-file broken the first time, then it could be that it uses the cached version of that zip-file after you fixed your routine and got a good zip.

Try to add:

  Response.Buffer = true;
  Response.Clear();
  Response.ClearContent();
  Response.ClearHeaders();

before your Response.ContentType and add this:

Response.Flush()
Response.Close()

before response.end, and see if that changes anything.

So the result is this:

Response.Buffer = true;
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/x-zip-compressed";  
Response.AppendHeader("Content-Disposition", "attachment; filename=SS.zip");  
Response.WriteFile(zipFilePath);  
Response.Flush()
Response.Close()
Response.End();

Just some tips just from the top of the mind.

Stefan
Tried it the same result. The zip file is corrupted!
azamsharp
A: 

And a long shot, try set the mime type to application/unknown, in this post there seems to be part of the solution to the posters problem:

http://jamesewelch.wordpress.com/2008/12/03/sharpziplib-and-windows-extraction-wizard-errors/

Stefan
A: 

I also tried to save the zip file to the server folder and then giving a link to the user to download it but it says the same "The file is corrupted". This is very strange since I can open the same folder if I visit my server folder and manually open it!

azamsharp
A: 

Here is another thing I found. If I save the zip file in the Server's folder and then reference it using the url: http://localhost/MyApplication/ServerFolder/MyZipFile.zip.

If I go to the url and download the zip file I get the same error "File is corrupted". But if I manually go to the folder using file explorer and open the file then it works as expected.

Why and How?

azamsharp
Tried to use another browser? I have seen different results with different browsers when other had that problem. And if IE, clear the cashe and try again.
Stefan
+1  A: 

I blogged about sending files like this a while ago. You might find something usefull in there.

http://absolutecobblers.blogspot.com/2008/02/downloading-and-deleting-temporary.html

Greg B