如何从C#.NET 4.5中的zip存档中只提取特定目录? [英] How to extract just the specific directory from a zip archive in C# .NET 4.5?

查看:143
本文介绍了如何从C#.NET 4.5中的zip存档中只提取特定目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有包含以下内部结构的zip文件:

I have zip file with following internal structure:

file1.txt
directoryABC
    fileA.txt
    fileB.txt
    fileC.txt

将文件从directoryABC文件夹提取到硬盘驱动器上的目标位置?例如,如果目标位置是C:\temp,则其内容应为:

What would be the best way to extract files from "directoryABC" folder to a target location on hard drive? For example if target location is "C:\temp" then its content should be:

temp
    directoryABC
        fileA.txt
        fileB.txt
        fileC.txt

在某些情况下,我只想提取directoryABC的内容,结果是:

Also in certain situations I'd want to extract only content of the "directoryABC" so the result would be:

temp
    fileA.txt
    fileB.txt
    fileC.txt

我如何使用C#.NET 4.5中的System.IO.Compression类实现这一点。

How can I accomplish this by using classes from System.IO.Compression in C# .NET 4.5?

推荐答案

的命名目录到目标目录...

This is another version to extract the files of a named directory to the target directory...

class Program
{
    static object lockObj = new object();

    static void Main(string[] args)
    {
        string zipPath = @"C:\Temp\Test\Test.zip";
        string extractPath = @"c:\Temp\xxx";
        string directory = "testabc";
        using (ZipArchive archive = ZipFile.OpenRead(zipPath))
        {
            var result = from currEntry in archive.Entries
                         where Path.GetDirectoryName(currEntry.FullName) == directory
                         where !String.IsNullOrEmpty(currEntry.Name)
                         select currEntry;


            foreach (ZipArchiveEntry entry in result)
            {
                entry.ExtractToFile(Path.Combine(extractPath, entry.Name));
            }
        } 
    }        
}

这篇关于如何从C#.NET 4.5中的zip存档中只提取特定目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆