简体   繁体   中英

Extracting a specific folder from a zip using DotNetZip

I've searched around for examples, but can't seem to find a DotNetZip scenario that involves extracting a certain folder. I'm trying to extract a folder called "CSS" from a .zip file, and it is a top-level folder inside the .zip file. This is the code I have so far:

using (ZipFile zip1 = ZipFile.Read(savedFileName))
{
    var selection = from e in zip1.Entries
                    where System.IO.Path.GetFileName(e.FileName).StartsWith("CSS/")
                    select e;

    foreach (var e in selection)
    e.Extract(_contentFolder);                
}

The current selection grabs nothing, and I could use some help rewriting it so it extracts the css folder and all of its subdirectories and files.

This worked for me.

          public void ExtractFiles(string fileName, string outputDirectory)
          {
                using (ZipFile zip1 = ZipFile.Read(fileName))
                {
                    var selection = (from e in zip1.Entries
                                     where (e.FileName).StartsWith("CSS/")
                                     select e);


                    Directory.CreateDirectory(outputDirectory);

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

Try this:

var entries = zip.SelectEntries("*", @"folder1\folder2\");
foreach (var file in entries)
{/* extract here */}

I think this is the best approach.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM