Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

End of Central Directory record could not be found

Tags:

c#

zip

zipfile

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

at System.IO.Compression.ZipArchive.ReadEndOfCentralDirectory()
   at System.IO.Compression.ZipArchive.Init(Stream stream, ZipArchiveMode mode,
Boolean leaveOpen)
   at System.IO.Compression.ZipArchive..ctor(Stream stream, ZipArchiveMode mode,
 Boolean leaveOpen, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.Open(String archiveFileName, ZipArchiveMode
mode, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileN
ame, String destinationDirectoryName, Encoding entryNameEncoding)
   at System.IO.Compression.ZipFile.ExtractToDirectory(String sourceArchiveFileN
ame, String destinationDirectoryName)

Here's the program

    response = (HttpWebResponse)request.GetResponse();
    Stream ReceiveStream = response.GetResponseStream();
    byte[] buffer = new byte[1024];
    FileStream outFile = new FileStream(zipFilePath, FileMode.Create);
    int bytesRead;
    while ((bytesRead = ReceiveStream.Read(buffer, 0, buffer.Length)) != 0)
        outFile.Write(buffer, 0, bytesRead);
    outFile.Close();
    response.Close();
    try
    {
        ZipFile.ExtractToDirectory(zipFilePath, destnDirectoryName);
    }
    catch (Exception e)
    {
        Console.WriteLine(e.ToString());
        Console.ReadLine();
    }

I do not understand the error. Can anybody explain this Thanks MR

like image 207
user2726975 Avatar asked Jan 06 '14 22:01

user2726975


People also ask

What is central directory in zip?

Zip file reader Basically what it does is download the central directory part of the . zip file which resides in the end of the file. Then it will read out every file and folder name with it's path from the bytes and print it out to console.


7 Answers

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

  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.
  2. The archive is corrupt.

    • Solution - The archive will need repairing.
  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/unzip utility (7-zip, winzip, etc) will tell which of these it could be.

like image 53
crthompson Avatar answered Oct 10 '22 06:10

crthompson


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.

like image 29
Scott Chamberlain Avatar answered Oct 10 '22 05:10

Scott Chamberlain


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
like image 43
SZL Avatar answered Oct 10 '22 05:10

SZL


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.

like image 23
Roger Hill Avatar answered Oct 10 '22 05:10

Roger Hill


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.

like image 29
Jason Roos Avatar answered Oct 10 '22 06:10

Jason Roos


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

like image 23
Raghu Avatar answered Oct 10 '22 05:10

Raghu


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.

like image 24
paulf Avatar answered Oct 10 '22 04:10

paulf