End of Central Directory record could not be found

喜夏-厌秋 提交于 2019-11-27 14:56:54

The problem is Unzip can't find the line of code that signals the end of the archive, so either:

  1. The archive is corrupt.

    • Solution - The archive will need repairing.
  2. It is not a .zip archive.

    • It may be a .rar or other compressed type. Or as I suspect here, you are downloading an html file that auto-redirects to the zip file.
    • Solution - Gotta find a correct archive to use this code.
  3. There is more than 1 part to the archive.
    • A multi part zip file.
    • Solution - Read in all the files before decompression.
  4. As @ElliotSchmelliot notes in comments, the file may be hidden or have extended characters in the name.
    • Solution - Check your file attributes/permissions and verify the file name.

Opening the file with your favorite zip utility will tell which of these it could be.

From your old question you deleted.

I get System.IO.InvalidDataException: End of Central Directory record could not be found.

This most likely means whatever file you are passing in is malformed and the Zip is failing. Since you already have the file outfile on the hard drive I would recommend trying to open that file with with windows built in zip extractor and see if it works. If it fails the problem is not with your unzipping code but with the data the server is sending to you.

I have the same problem, but in my case the problem is with the compression part and not with the decompression.

During the compression I need use the "Using" statament with the Stream and the ZipArchive objects too. The "Using" statament will Close the archive properly and I can decompress it without any problem.

The working code in my case in VB.Net:

Using zipSteramToCreate As New MemoryStream()
    Using archive As New ZipArchive(zipSteramToCreate, ZipArchiveMode.Create)
        ' Add entry...
    End Using

    ' Return the zip byte array for example:
    Return zipSteramToCreate.ToArray
End Using

I encountered this same problem. There are many types of compression, .zip being only one of the types. Look and make sure that you aren't trying to 'unzip' a .rar or similar file.

I just came across this thread when I had the same error from a PowerShell script calling the Net.WebClient DownloadFile method.

In my case, the problem was that the web server was unable to provide the requested zip file, and instead provided an HTML page with an error message in it, which obviously could not be unzipped.

So instead, I created an exception handler to extract and present the "real" error message.

I used SharpCompress C#.net Library available via Nuget Package manager, it solved my purpose of unzipping.

Might be useful to someone else. I dealt with this by adding an exception to my code, which then:

  1. Creates a temporary directory
  2. Extracts the zip archive (normally works)
  3. Renames the original ziparchive to *.bak
  4. Zips and replaces the original archive file with one that works
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!