c# - 如何从 C# .NET 4.5 中的 zip 存档中提取特定目录?

标签 c# .net compression zip

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

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

将文件从“directoryABC”文件夹提取到硬盘上的目标位置的最佳方法是什么?例如,如果目标位置是“C:\temp”,那么它的内容应该是:

temp
    directoryABC
        fileA.txt
        fileB.txt
        fileC.txt

同样在某些情况下,我只想提取“directoryABC”的内容,因此结果将是:

temp
    fileA.txt
    fileB.txt
    fileC.txt

如何通过在 C# .NET 4.5 中使用 System.IO.Compression 中的类来完成此操作?

最佳答案

这是将指定目录的文件提取到目标目录的另一个版本...

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# - 如何从 C# .NET 4.5 中的 zip 存档中提取特定目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22133053/

相关文章:

c# - Console.WriteLine() 与 Console.WriteLine(string.Empty)

c# - LINQ 根据 IList<int> 从 IList<T> 中删除某些元素

c# - Oracle 根据浏览器文化返回日期时间格式

.net - WCF NetTcpBinding 双工 channel 端点可以在端口 80 上吗?

c# - 在没有 Try Catch 的情况下检查文件锁定

c++ - LZF 可能会使用不同的算法进行压缩

c# - 复选框上的数据绑定(bind)

.net - 在 .NET 中使用 C++/CLI 实用程序 :

java - 有没有更好的方法可以对Java集合对象实现有值(value)的压缩?

Swift5 播放 aac 文件 AvAudioPlayer