简体   繁体   English

Ionic Zip Extract仅限特定文件夹

[英]Ionic Zip Extract only particular folder

I have a case, where I need to extract Zip file with C# Ionic.zip library. 我有一个案例,我需要用C#Ionic.zip库提取Zip文件。 Zip file contains multiple folders and I want to extract and copy a particular folder to specific destination. Zip文件包含多个文件夹,我想将特定文件夹提取并复制到特定目标。

eg Zip file named as abc.zip and directory structure will be like 例如,名为abc.zip的Zip文件和目录结构就像

Parent Directory->Sub directory 1->file a, file b Parent Directory->Sub directory 2->file c, file d 父目录 - >子目录1->文件a,文件b父目录 - >子目录2->文件c,文件d

I just want to copy Sub Directory 1, how can I accomplish this task? 我只想复制子目录1,我该如何完成这项任务?

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

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. 虽然这可能比枚举所有条目然后过滤它们更快,但它具有不提取空文件夹的缺点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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