locked
Zip file Corrupt error when using Response.BinaryWrite RRS feed

  • Question

  • User-1765822410 posted

    I am trying to export data in a zip file but not creating in server instead writing the bytes.

    Below is the code, i have written. There is no exception, but when i open the file from open save file dialog, it says the file is corrupted.


           

    ZipOutputStream objZipOutputStream = null;
            ZipEntry objZipEntry = null;
            MemoryStream objStream = new MemoryStream();
            byte[] data = null;
            int streamLength = 0;
            try
            {
                objZipOutputStream = new ZipOutputStream(objStream);
                for (int i = 0; i < objKMZs.Count; i++)
                {
                    data = objKMZs[i] as byte[];
                    objZipEntry = new ZipEntry("KMZ" + i + ".KMZ");
                    objZipEntry.Size = data.Length;
                    objZipOutputStream.PutNextEntry(objZipEntry);
                    objZipOutputStream.Write(data, 0, data.Length);
                }
                streamLength =int.Parse(objZipOutputStream.Length.ToString());
                if (objZipOutputStream != null)
                {
                    objZipOutputStream.Finish();
                }
            }
            catch
            {
                if (objZipOutputStream != null)
                {
                    objZipOutputStream.Finish();
                }
                throw;
            }
            
             data = new byte[streamLength];
             objStream.Read(data, 0, streamLength);
             objZipOutputStream.Dispose();
            Response.Expires = -1;
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("content-disposition", "attachment;filename=data.zip");
            Response.BinaryWrite(data);
              Response.End();
            Response.Flush();
            Response.Clear();
    
    Thursday, September 15, 2011 6:04 PM

All replies

  • User753101303 posted

    Hi,

    Try to change the MemoryStream position to 0 before reading it? My approach to debug this kind of thing is to try with the smallest file I can , save it on the server, download it and compare size and content...

    For now I suspect the file is empty?

    Friday, January 23, 2015 4:33 AM