【发布时间】:2020-12-18 13:54:15
【问题描述】:
我正在尝试解压缩一个目录中的所有 zip,其中有时包含多层嵌套的 zip。我还想在提取的版本中保留原始的 zip 嵌套结构。我可以用一层拉链做我想做的事,但不能用于嵌套拉链或子文件夹内的拉链。
例如,我想将起始文件夹中的所有内容都变成这样:
[Starting Folder]
-Zip1.zip
--NestedZip1.zip
---text1.txt
--NestedZip2.zip
---text2.txt
-Zip2.zip
--[Subfolder1]
---Zip3.zip
----text3.txt
---Zip4.zip
----text4.txt
然后将所有内容提取到同一起始目录中的文件夹中:
[Starting Folder]
[Extracted Folder]
-Zip1 (folder)
--NestedZip1 (folder)
---text1.txt
--NestedZip2 (folder)
---text2.txt
-Zip2 (folder)
--[Subfolder1]
---Zip3 (folder)
----text3.txt
---Zip4 (folder)
----text4.txt
现在我正在使用它来解压缩 MyGlobals.finalPathForWork(这是起始目录)中的所有文件,它可以工作,但只解压缩一层拉链。我需要它以某种方式再次运行,以防第一层拉链中有拉链。
public static void MyMethod3()
{
string startPath = MyGlobals.finalPathForWork;
string extractPath = MyGlobals.finalPathForWork + @"\\Extracted\";
Directory.GetFiles(startPath, "*.zip", SearchOption.AllDirectories).ToList()
.ForEach(zipFilePath =>
{
var extractPathForCurrentZip = Path.Combine(extractPath, Path.GetFileNameWithoutExtension(zipFilePath));
if (!Directory.Exists(extractPathForCurrentZip))
{
Directory.CreateDirectory(extractPathForCurrentZip);
}
ZipFile.ExtractToDirectory(zipFilePath, extractPathForCurrentZip);
});
}
我尝试过应用/组合以下内容:How to unzip multi layered zip files in C#
public static void ExtractFile(string baseZipPath, string extractPath)
{
if (!Directory.Exists(extractPath))
Directory.CreateDirectory(extractPath);
ZipFile.ExtractToDirectory(baseZipPath, extractPath);
string[] nestedZipDirectories = System.IO.Directory.GetFiles(extractPath, "*.zip");
foreach (var nestedZipDirectory in nestedZipDirectories)
{
ZipFile.ExtractToDirectory(nestedZipDirectory, extractPath);
}
}
static void Main(string[] args)
{
ExtractFile(@"c:\myfolder\grandfather.zip", @"c:\myfolder2");
}
是否有另一种方法可以通过所有子文件夹和嵌套的 zip 文件循环搜索/解压缩过程?或者上面的其他解决方案是否应该有效,而我必须错误地合并它?
【问题讨论】: