Error in Windows Azure when uploading a zip file: “ZipException was unhandled” “EOF in header”

StackOverflow https://stackoverflow.com/questions/2547481

  •  23-09-2019
  •  | 
  •  

Question

I've been using Windows Azure to create a document management system, and things have gone well so far. I've been able to upload and download files to the BLOB storage through an asp.net front end.

What I'm attempting to do now is allow users to upload a .zip file, and then take the files out of that .zip and save them as individual files. Problem is, I'm getting "ZipException was unhandled" "EOF in header" and I don't know why.

I'm using the ICSharpCode.SharpZipLib library which I've used for many other tasks and its worked great.

Here's the basic code:

CloudBlob ZipFile = container.GetBlobReference(blobURI);
MemoryStream MemStream = new MemoryStream();
ZipFile.DownloadToStream(MemStream);
....
while ((theEntry = zipInput.GetNextEntry()) != null)

and it's on the line that starts with while that I get the error. I added a sleep duration of 10 seconds just to make sure enough time had gone by.

MemStream has a length if I debug it, but the zipInput does sometimes, but not always. It always fails.

Was it helpful?

Solution

Just a random guess, but do you need to seek the stream back to 0 before you read it? Not sure if you're doing that already (or if it's necessary).

OTHER TIPS

@Smarx hint did the trick for me too. The key to avoiding empty files inside the zip is to set the position to zero. For the sake of clarity here is sample code that sends a zip-stream containing an Azure blob to the browser.

        var fs1 = new MemoryStream();
        Container.GetBlobReference(blobUri).DownloadToStream(fs1);
        fs1.Position = 0;

        var outputMemStream = new MemoryStream();
        var zipStream = new ZipOutputStream(outputMemStream);

        var entry1 = new ZipEntry(fileName);
        zipStream.PutNextEntry(entry1);
        StreamUtils.Copy(fs1, zipStream, new byte[4096]);
        zipStream.CloseEntry();

        zipStream.IsStreamOwner = false;    // False stops the Close also Closing the underlying stream.
        zipStream.Close();                  // Must finish the ZipOutputStream before using outputMemStream.

        outputMemStream.Position = 0;

        Response.Clear();
        Response.AddHeader("Content-Disposition", "attachment;filename=" + zipFileName);
        Response.OutputStream.Write(outputMemStream.ToArray(), 0, outputMemStream.ToArray().Length);
        Response.End();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top