Search code examples
c#serverzip

Zip file is corrupted when downloaded from server C#


I have a zipped a file and while downloading it from the server using C# code it is showing invalid while extracting, I have tried extracting the file directly from server and it was working.

So, here the file seems to be corrupted only during download. This was working correctly a few days before but now it is showing issue.

Can you please check my code and tell what could be the problem here, the same code works correctly in my local development environment.

Already tried,

1.Tried extracting the file directly in server and it worked , so issue must be with my downloading code.

2.The same download code is working correctly.

Note : File is already zipped in the server and the following code just downloads the zipped file, zipping is fine as file is opening in server

protected void linkbtntempdownload_Click(object sender,EventArgs e)
            {
                string downloadlocation = string.Empty;
                if(ConfigurationManager.AppSettings["Environment"] == "prod")
                {
                    downloadlocation = ConfigurationManager.AppSettings["Templates"].ToString();
                }else
                {
                    downloadlocation = Server.MapPath("~/TemplateFolder/Templates.zip");
                }

                Response.ContentType = "application/zip";
                Response.AddHeader("content-disposition", "attachment; filename=" + "_Templates.Zip");

                Response.WriteFile(downloadlocation);
                Response.Flush();
                Response.SuppressContent = true;
                HttpContext.Current.ApplicationInstance.CompleteRequest();



            }

Solution

  • Hi was able to rectify the issue finally. The reason was that the zip file was corrupted.

    when zip file is opened with notepad it should start with P. But when opened our file we found an additional string was appended to the file. we were able to find that a Respose.write("string") was present in our server code that was appending as the additional string in the zip file. After removing it the file opened properly.