End of Central Directory record could not be found

后端 未结 10 1853
温柔的废话
温柔的废话 2020-11-29 10:39

I am downloading a zip file using c# program and I get the error

at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
   at System.IO.Compression         


        
相关标签:
10条回答
  • 2020-11-29 11:20

    The problem is ZipFile 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.
    1. 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.
    1. There is more than 1 part to the archive.
      • A multi part zip file.
      • Solution - Read in all the files before decompression.
    2. 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/unzip utility (7-zip, winzip, etc) will tell which of these it could be.

    0 讨论(0)
  • 2020-11-29 11:20

    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
    
    0 讨论(0)
  • 2020-11-29 11:25

    I found resolution.

    Move "Tools->Nuget PackageManager ->Package Manager Settings" and in "Nuget Package Manager" -General Tab , click Clear All Nuget Caches button and OK. You can install package from online

    0 讨论(0)
  • 2020-11-29 11:28

    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
    0 讨论(0)
  • 2020-11-29 11:29

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

    0 讨论(0)
  • 2020-11-29 11:31

    In my case i absolutely KNEW that my zip was not corrupted, and I was able to figure out through trial and error that I was extracting the files to a directory with the filename and extension in the FOLDER Name.

    So Unzipping /tmp/data.zip to:

    /tmp/staging/data.zip/files_go_here

    failed with the error [End of Central Directory record could not be found]

    but extracting data.zip to this worked just fine:

    /tmp/staging/data/files_go_here

    While it might seem unusual to some folks to name a folder a filename with extension, I can't think of a single reason why you should not be able to do this, and more importantly -- the error returned is not obviously related to the cause.

    I was getting the same error with both the System.IO.Compression library and 3rd party packages such as SharpZipLib -- which is what eventually clued me in that it was a more general issue.

    I hope this helps someone and saves them some time/frustration.

    0 讨论(0)
提交回复
热议问题