C#来解压缩目录中所有嵌套的zip,同时保留文件结构



我正在尝试解压缩一个目录中的所有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(这是起始目录(中的所有文件,它可以工作,但只解压缩一层zip。我需要它以某种方式再次运行,以防第一层拉链中有拉链。

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);
});
}

我尝试过应用/组合这些内容:如何在C#中解压缩多层zip文件

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:myfoldergrandfather.zip", @"c:myfolder2");
}

是否有其他方法可以通过所有子文件夹和嵌套的zip文件循环搜索/解压缩过程?或者上面的另一个解决方案应该有效吗?我一定只是错误地结合了它?

这是递归的经典用例
注意代码中的注释:

public static void ExtractFile(string zipFilePath, string extractPath)
{
// If directory already exist, CreateDirectory does nothing
Directory.CreateDirectory(extractPath); 
// Extract current zip file
ZipFile.ExtractToDirectory(zipFilePath, extractPath);
// Enumerate nested zip files
var nestedZipFiles = Directory.EnumerateFiles(extractPath, "*.zip");
// iterate the enumerator
foreach (var nestedZipFile in nestedZipFiles)
{    
// Get the nested zip full path + it's file name without the ".zip" extension
// I.E this "C:usersYourUserNameDocumentsSomeZipFile.Zip" - turns to this: "C:usersYourUserNameDocumentsSomeZipFile".
var nestedZipExtractPath = Path.Combine(Path.GetDirectoryName(nestedZipFile), Path.GetFileNameWithoutExtension(nestedZipFile));
// extract recursively
ExtractFile(nestedZipFile, nestedZipExtractPath);
}
}
//Here is the complete working & tested code. Before running, just change the zipPath and extractPath.
static void Main(string[] args)
{
string zipPath = @"C:TestZipFilestest.zip"; 
string extractPath = @"C:TestZipFilesextractfiles";
int dbFileCount = 0;
ExtractRootFile(zipPath, extractPath, ref dbFileCount);
}
private static void DeleteSubFolderAndFiles(string destinationPath)
{
string[] fileEntries = Directory.GetFiles(destinationPath);
foreach (string fileName in fileEntries)
File.Delete(fileName);
string[] subdirectoryEntries = 
Directory.GetDirectories(destinationPath);
foreach (string subdirectory in subdirectoryEntries)
{
DeleteSubFolderAndFiles(subdirectory);
if (Directory.GetFiles(subdirectory).Length == 0 && 
Directory.GetDirectories(subdirectory).Length == 0)
{
Directory.Delete(subdirectory);
}
}
}
public static void ExtractRootFile(string baseZipPath, string extractPath, ref int dbFileCount)
{
if (Directory.Exists(extractPath))
DeleteSubFolderAndFiles(extractPath);
if (!Directory.Exists(extractPath))
Directory.CreateDirectory(extractPath);
ZipFile.ExtractToDirectory(baseZipPath, extractPath);
extractPath = extractPath + @"test"; //replace this with dynamic value of first zip file only.
RecursivelyExtractChildFolders(extractPath, ref dbFileCount);
}
public static void RecursivelyExtractChildFolders(string extractPath, ref int dbFileCount)
{
string[] nestedZipDirectories = System.IO.Directory.GetFiles(extractPath, "*.zip");
string[] nestedDBDirectories = System.IO.Directory.GetFiles(extractPath, "*.db");
if (nestedDBDirectories.Length > 0)
dbFileCount = dbFileCount + nestedDBDirectories.Length;
foreach (var nestedZipDirectory in nestedZipDirectories)
{
ZipFile.ExtractToDirectory(nestedZipDirectory, extractPath);
}
if (Directory.Exists(extractPath))
{
string[] subdirectoryEntries = 
Directory.GetDirectories(extractPath);
foreach (string subdirectory in subdirectoryEntries)
{
RecursivelyExtractChildFolders(subdirectory, ref dbFileCount);
}
}
}

相关内容

最新更新