mortenstarck
mortenstarck

Reputation: 2803

Extract specific subfolder from Zip

I have a zip file where in the root, contains only two folders: Binaries and Source But i only need the Binaries folder. Have is this possible in C#? This is the code i have currently, but is not working.

using (ZipArchive archive = ZipFile.OpenRead(zipPath.FullName))
{
     foreach (ZipArchiveEntry entry in archive.Entries)
     {
        if (entry.FullName.StartsWith(@"Binaries/", StringComparison.OrdinalIgnoreCase))
        {
             entry.ExtractToFile(Path.Combine(extractPath, "Hepper"));
        }
     }
} 

UPDATE: It gives me this error The file

'C:\Hepper\Hepper' already exists.

Upvotes: 0

Views: 2058

Answers (2)

mortenstarck
mortenstarck

Reputation: 2803

I found an solution to my problem:

using (Ionic.Zip.ZipFile zip1 = Ionic.Zip.ZipFile.Read(path.FullName))
{
       var selection = (from e in zip1.Entries
                      where (e.FileName).StartsWith("Binaries/")
                                 select e);


     Directory.CreateDirectory(_localExtratingPath.FullName);

     foreach (var e in selection)
     {
         e.Extract(_localExtratingPath.FullName);
     }
}

Upvotes: 0

Chris
Chris

Reputation: 27619

At a glance (without actually checkin the API) it looks like you are trying to extract all entries to the same filename (Path.Combine(extractPath, "Hepper")). You'd probably want to have the path and filename from the entry as part of where you are extracting to.

Upvotes: 1

Related Questions