Central Directory corrupt error in ziparchive

霸气de小男生 提交于 2019-12-22 04:13:13

问题


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:


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);
 }


来源:https://stackoverflow.com/questions/33687425/central-directory-corrupt-error-in-ziparchive

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!