adamhathcock / sharpcompress

SharpCompress is a fully managed C# library to deal with many compression types and formats.
MIT License
2.25k stars 480 forks source link

Missing root folder as directory entry in results of MoveToNextEntry #267

Open KvanTTT opened 7 years ago

KvanTTT commented 7 years ago

I use the following code for archive bypassing:

using (Stream stream = File.OpenRead(sourceArchiveFileName))
{
    using (IReader reader = ReaderFactory.Open(stream))
    {  
        while (reader.MoveToNextEntry())
        {
            string path = reader.Entry.Key;

            // Expected behavior
            // Create directory only for directory items
            if (entry.IsDirectory)
            {
                Directory.CreateDirectory(Path.Combine(destDir, path));
                continue;
            }

            // Actual workaround: extract directory path and create it for each file entry 
            Directory.CreateDirectory(
                Path.Combine(destDir, Path.GetDirectoryName(path)));

            // Write file item to stream
            using (var sourceFileStream = new FileStream(Path.Combine(destDir, path), FileMode.Create))
            {
                reader.WriteEntryTo(sourceFileStream);
            }
        }
    }
}

And get the following result items for attached test archive Folder.zip obtained from standard Windows zip utility (7z does not have such issue):

Actual result items

Folder/Folder2/
Folder/Folder2/text.txt
Folder/text.txt

But it should be

Expected result items

Folder/
Folder/Folder2/
Folder/Folder2/text.txt
Folder/text.txt
adamhathcock commented 7 years ago

I get the same actual result items. Whatever made that file did not actually put an entry in the zip archive for Folder/ In fact, having non-empty directories is redundant as you'd have to make them anyways to extract file