15
votes

In my c# code I am trying to create a zip folder for the user to download in the browser. So the idea here is that the user clicks on the download button and he gets a zip folder.

For testing purpose I am using a single file and zipping it but when it works I will have multiple files.

Here is my code

var outPutDirectory = AppDomain.CurrentDomain.BaseDirectory;
string logoimage = Path.Combine(outPutDirectory, "images\\error.png"); // I get the file to be zipped

HttpContext.Current.Response.Clear();
HttpContext.Current.Response.BufferOutput = false;
HttpContext.Current.Response.ContentType = "application/zip";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=pauls_chapel_audio.zip");


using (MemoryStream ms = new MemoryStream())
     {
          // create new ZIP archive within prepared MemoryStream
          using (ZipArchive zip = new ZipArchive(ms))
             {
                    zip.CreateEntry(logoimage);
                    // add some files to ZIP archive

                    ms.WriteTo(HttpContext.Current.Response.OutputStream);
             }
     }

When I try this thing it gives me this error

Central Directory corrupt.

[System.IO.IOException] = {"An attempt was made to move the position before the beginning of the stream."}

Exception occurs at

using (ZipArchive zip = new ZipArchive(ms))

Any thoughts?

1
Just a note if you are trying to unzip files: If the zip is located in your OneDrive folder and OneDrive is turned off and the file is set to online mode, you'll get the same error. Just turn OneDrive on again and it should work.Nicke Manarin

1 Answers

29
votes

You're creating the ZipArchive without specifying a mode, which means it's trying to read from it first, but there's nothing to read. You can solve that by specifying ZipArchiveMode.Create in the constructor call.

Another problem is that you're writing the MemoryStream to the output before closing the ZipArchive... which means that the ZipArchive code hasn't had a chance to do any house-keeping. You need to move the writing part to after the nested using statement - but note that you need to change how you're creating the ZipArchive to leave the stream open:

using (MemoryStream ms = new MemoryStream())
{
    // Create new ZIP archive within prepared MemoryStream
    using (ZipArchive zip = new ZipArchive(ms, ZipArchiveMode.Create, true))
    {
        zip.CreateEntry(logoimage);
        // ...
    }        
    ms.WriteTo(HttpContext.Current.Response.OutputStream);
 }