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

时间:2020-08-30 04:30:23

标签: c# unzip

我正在尝试解压缩目录中的所有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);
            });
    }

我尝试应用/组合以下内容: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文件来进行搜索/解压缩过程?还是上述其他解决方案应该起作用,而我必须只是错误地合并了它?

1 个答案:

答案 0 :(得分:4)

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

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:\users\YourUserName\Documents\SomeZipFile.Zip" - turns to this: "C:\users\YourUserName\Documents\SomeZipFile".
        var nestedZipExtractPath = Path.Combine(Path.GetDirectoryName(nestedZipFile), Path.GetFileNameWithoutExtension(nestedZipFile));

        // extract recursively
        ExtractFile(nestedZipFile, nestedZipExtractPath);
    }
}
相关问题