Reputation: 115
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.
Lets say i have directory c:\myfolder\grandfather.zip\father.zip\child.zip\familyimage.jpg.
I want to count all files underneath these zip files. and extract them into another location.
How do i do it in C# .Net
Upvotes: 0
Views: 1121
Reputation: 99
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);
}
}
Then :
static void Main(string[] args)
{
ExtractFile(@"c:\myfolder\grandfather.zip", @"c:\myfolder2");
}
Upvotes: 1