gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 09:05:14 26 4
gpt4 key购买 nike

我有具有以下内部结构的 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com