Search code examples
c#exceptionextractzip

Zip entry name ends in directory separator character but contains data


System.IO.Compression.ZipFile.ExtractToDirectory(zipPath, extractPath);

Using ZipFile in C#, I'm trying to extract a file from a known location and it is throwing the following error: System.IO.Exception: Zip entry name ends in directory separator character but contains data

I've done some research, ExtractToDirectory is explained in MSDN but couldn't find this error definition. Would you be able to explain why this error is happening?

From MSDN:

IOException

The directory specified by destinationDirectoryName already exists.

-or-

The name of an entry in the archive is Empty, contains only white space, or contains at least one invalid character.

-or-

Extracting an archive entry would create a file that is outside the directory specified by destinationDirectoryName. (For example, this might happen if the entry name contains parent directory accessors.)

-or-

An archive entry to extract has the same name as an entry that has already been extracted from the same archive.


Solution

  • My research shows that when you compress the files with 7-Zip utility using "Ultra" compression level, certain archives cannot be unzipped with .NET System.IO.Compression.ZipFile.ExtractToDirectory() method. The error message is: Zip entry name ends in directory separator character but contains data.

    Based on my observation, this error occurs only if the following 3 conditions are met:

    1. The target archive is large - 500MB+ (Small archives seem to unzip fine).
    2. The root of the archive contains ONLY FOLDERS (if you add at least one file to the root of the archive it also unzips fine).
    3. 7-Zip Ultra compression is used.

    In the end I found three solutions to this 7-Zip bug.

    Solution 1: Add at least one file to the root of the archive (any dummy file or a readme.txt file).

    Solution 2: Use native Windows compression utility (Select files, right-click, Send To -> Compressed folder).

    Solution 3: Do not use 7-Zip Ultra compression.

    All of these solutions resolved the problem for me. Pick the one that fits your project best.