Extracting a specific folder from a zip using DotNetZip

江枫思渺然 提交于 2019-11-30 15:52:59

问题


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.


回答1:


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);        
                    }
                }
         }



回答2:


Try this:

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

I think this is the best approach.



来源:https://stackoverflow.com/questions/4788386/extracting-a-specific-folder-from-a-zip-using-dotnetzip

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!