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