0

Problem: I have this code that extracts all .zip folder inside a specified directory. Now my problem here is that my .zip files contains another .zip file inside of it. The output of my program is it creates a folder for the zip file extracted with a naming structure like this NUMBER_ + ZipFileName , now when I open the folder, it still contains a .zip folder inside. How do I extract a zip folder within a zip folder on the same NUMBER_ + ZipFileName Folder? It's kinda confusing to me.

desired file structure

Temp - > number_zipfilename-> [ extracted files here w/o .zip]

number_zipfilename2 - > [extraced files here w/o .zip]

.....

output of my script file structure

Temp - > number_zipfilename -> [extracted files but with .zip ]

number_zipfilename -> [extracted files but with .zip ]

Tried the recursive suggestion, but it creates another folder inside my number_zipfilename folder and the .zip file is still inside the folder.

this is my tasks requirements, its kinda hard to grasp.

this is my script

public void extractZipFiles(string targetFileDirectory, string zipFileDirectory, string Number)
{
            Directory.GetFiles(zipFileDirectory, "*.zip", SearchOption.AllDirectories).ToList()
            .ForEach(zipFilePath => {
                var test = Number + "_" + Path.GetFileNameWithoutExtension(zipFilePath);
                var extractPathForCurrentZip = Path.Combine(targetFileDirectory, test);
                if(!Directory.Exists(extractPathForCurrentZip))
                {
                    Directory.CreateDirectory(extractPathForCurrentZip);
                }
                ZipFile.ExtractToDirectory(zipFilePath, extractPathForCurrentZip);
            });
}
devbbg
  • 17
  • 7
  • 1
    do you want to recursively unzip? like ... call your routine again for every unzipped zip file? ... oh and by the way ... are you aware of 42.zip? it's a 42KB zip file containing zip files that expands to roughly 4.5 petabyte if unzipped recursively ... https://en.wikipedia.org/wiki/Zip_bomb – DarkSquirrel42 May 18 '20 at 06:01
  • @DarkSquirrel42 thanks for the heads up! this trial only consists of .zip files though, thanks again! :) – devbbg May 18 '20 at 06:19

1 Answers1

0

As per what I see in your code, I think you should call the extractZipFiles method recursively, so that after the extracting you call the method again with the directory where you extracted the files so it scans it for *.zip files.

I'm not sure about what variables you'd want to use, but something like this:

public void extractZipFiles(string targetFileDirectory, string zipFileDirectory, string Number)
{
            Directory.GetFiles(zipFileDirectory, "*.zip", SearchOption.AllDirectories).ToList()
            .ForEach(zipFilePath => {
                var test = Number + "_" + Path.GetFileNameWithoutExtension(zipFilePath);
                var extractPathForCurrentZip = Path.Combine(targetFileDirectory, test);
                if(!Directory.Exists(extractPathForCurrentZip))
                {
                    Directory.CreateDirectory(extractPathForCurrentZip);
                }
                ZipFile.ExtractToDirectory(zipFilePath, extractPathForCurrentZip);
                extractZipFiles(targetFileDirectory, extractPathForCurrentZip, Number);
            });
}
devcrp
  • 1,323
  • 7
  • 17
  • that did the trick! but if possible , how can I edit my code out to be able to save the zip inside my zip file just inside the folder ? Doing it recursively will create another folder for the zip folder inside, I want all the files inside 1 folder... with Number_ + file name. Thank you so much!! I think I need to modify the codes of my method for this to work. – devbbg May 18 '20 at 06:12
  • I just edited my code changing the first param (I think that would do it). Hope it helps! – devcrp May 18 '20 at 06:23
  • appreciate the help bro! and to realizing what I need to do to reach my programming goal. :) it helped a lot. – devbbg May 18 '20 at 06:54
  • @kebbg nice! if you think that answers your question please would you accept my answer? Glad it helped! – devcrp May 18 '20 at 07:10
  • your suggestion is half way there, but im still not getting to my desired output :( I upvoted your answer though mister devcrp – devbbg May 18 '20 at 07:19