3
votes

I have a case, where I need to extract Zip file with C# Ionic.zip library. Zip file contains multiple folders and I want to extract and copy a particular folder to specific destination.

e.g. Zip file named as abc.zip and directory structure will be like

Parent Directory->Sub directory 1->file a, file b Parent Directory->Sub directory 2->file c, file d

I just want to copy Sub Directory 1, how can I accomplish this task?

2

2 Answers

3
votes
        var existingZipFile = "name of the file.zip";
        var targetDirectory = "name of the folder";

        using (ZipFile zip = ZipFile.Read(existingZipFile))
        {
            foreach (ZipEntry e in zip.Where(x => x.FileName.StartsWith("Sub directory 1")))
            {
                e.Extract(targetDirectory);
            }
        }
0
votes

Here's another possible solution:

using (ZipFile zip = ZipFile.Read(sourceFile))
{
    zip.ExtractSelectedEntries("name = *", "My sub directory", targetPath, ExtractExistingFileAction.OverwriteSilently);
}

While this may be faster then enumerating all entries and then filtering them, it has however, the downside of not extracting empty folders.