如何在 C# 中解压缩多层 zip 文件 [英] How to unzip multi layered zip files in C#

查看:77
本文介绍了如何在 C# 中解压缩多层 zip 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道如何解压缩一个压缩文件,并且有解决堆栈溢出问题的解决方案.但是我没有找到在 zip 文件中有多层 zip 文件的解决方案.

I know how to unzip a zipped file and there are solutions for the problem here in stack overflow. But i did not find a solutions where there is multi layers of zip files in zip files.

假设我有目录 c:\myfolder\grandfather.zip\father.zip\child.zip\familyimage.jpg.

Lets say i have directory c:\myfolder\grandfather.zip\father.zip\child.zip\familyimage.jpg.

我想计算这些 zip 文件下的所有文件.并将它们提取到另一个位置.

I want to count all files underneath these zip files. and extract them into another location.

我如何在 C# .Net 中做到这一点

How do i do it in C# .Net

推荐答案

您可以解压一次,循环遍历所有文件,如果提取的文件扩展名为 *.zip,则您可以再次解压.由于听起来您知道如何解压缩一次,因此发布嵌套解压缩只是逻辑编码.您可以使用此方法:

You can unzip it once, loop through all the files and if any extracted file extension is *.zip then you can unzip it again. Since it sounds like you know how to unzip once, post that it's just logical coding to have nested unzip. You can use this method :

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

这篇关于如何在 C# 中解压缩多层 zip 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆