2
votes

Im trying to get Ionic zip to extract a folder inside a zip file into a specified root directory. My problem is that the zip files inside is "zipfile.zip\some_folder\". I want to extract all the files within "some_folder" to another directory root.

Heres what i have done

using (ZipFile zip = ZipFile.Read("zipfile.zip"))
{
var selection = (from cf in zip.Entries
                 where (cf.FileName).StartsWith("some_folder/")
                 select cf);

foreach (var cf in selection)
    {
    try
        {
            cf.Extract(destinationPath, ExtractExistingFileAction.OverwriteSilently);
            progress.Value = progress.Value + 1;
        }
        catch (Exception ex)
        {
            installProblems = true;
        }

    }
}

The problem with the above code is that it extracts the folder it self too into "destinationPath\some_folder" .. I want the files from "zipfile.zip\some_folder\" to be extracted in "destinationPath\"

Hope you can assist!

EDIT :

So i tried out some few things, and came up with something that works, but i dont like it though:

using (ZipFile zip = ZipFile.Read("zipfile.zip"))
{
    var selection = (from cf in zip.Entries
                                 where (cf.FileName).StartsWith("some_folder/")
                                 select cf);

    selection.ToList().ForEach(entry =>
    {
        try
        {
            entry.FileName = entry.FileName.Substring(12);
            entry.Extract(minecraftPathVar, ExtractExistingFileAction.OverwriteSilently);
        }
        catch
        {
            installProblems = true;
        }

    });
}

The above works, but i dont believe its a good practice even though the name "some_folder" is not changed at any time, i just dont like this kind of approach..

1

1 Answers

5
votes

Right after your first line: using (ZipFile zip = ZipFile.Read("zipfile.zip"))

Try doing this: zip.FlattenFoldersOnExtract = true