简体   繁体   中英

C# - Unzip all files in directory and its subdirectories

I try to extract all ZIP archives in their respective directory / subdirectory to their respective directory / subdirectory.

Example of the structure:

-Root-Directory
  - File.txt
  - Fi.docx
  - Files.zip
      - TestEx.jpg
      - Example.png
  - /Subdirectory_1
      - ExampleData.txt
      - Example.xlsx
      - Archive1.zip
           - ExampleData_1.txt
           - ExampleData_2.txt
      - Archiv2.zip
           - Data1.txt
           - Data2.txt
      - ...
  - /Subdirectory_2
      - T_1.txt
      - ABC.xlsx
      - ZippedFiles.zip
           - Imag_1.jpeg
           - Imag_2.gif
      - ZipFiles.zip
           - Music.wav
           - Sound.mp3

How it should look like:

-Root-Directory
  - File.txt
  - Fi.docx
  - TestEx.jpg
  - Example.png
  - /Subdirectory_1
      - ExampleData.txt
      - Example.xlsx
      - ExampleData_1.txt
      - ExampleData_2.txt
      - Data1.txt
      - Data2.txt
      - ...
  - /Subdirectory_2
      - T_1.txt
      - ABC.xlsx
      - Imag_1.jpeg
      - Imag_2.gif
      - Music.wav
      - Sound.mp3

My attempt:

private static void unzipAllFiles(DirectoryInfo directoryPath)
        {
            foreach( DirectoryInfo dirs in directoryPath.GetDirectories() )
            {
                foreach (FileInfo file in dirs.GetFiles())
                {   
                    if(Path.GetExtension(file.FullName).Equals(".zip"))
                    {
                        //Console.WriteLine(file.FullName);
                        string zPath = @"C:\Program Files\7-Zip\7z.exe";// change the path and give yours 
                        try
                        {
                            ProcessStartInfo pro = new ProcessStartInfo();
                            pro.WindowStyle = ProcessWindowStyle.Hidden;
                            pro.FileName = zPath;
                            pro.Arguments = "x -r -aou \"" + file.FullName;
                            Process process = Process.Start(pro);
                            process.WaitForExit();
                        }
                        catch (System.Exception Ex)
                        {
                            //DO logic here 
                        }
                    }
                }
            }
        }

A few things here:

  1. How to surpress the opening of the cmd.exe ?
  2. It does not even iterate through the ZIP archives in the root directory. How to fix this?
  3. Although the output of 7zip on cmd.exe prints that some extracting is processed, nothing happens. How to fix that?

Thanks in advance.

Try writing a method that extracts a zip then iterates over its extracted contents to look for and extract any other zip files. You can use the ZipFile, FileInfo, and DirectoryInfo classes. Here's some psuedo code...

func extractZip
    // extract zip file
    // call extractZipsFromDir using base extract path

func extractZipsFromDir
    // get list of all .zip files in cur dir
    // while list.len > 0
        // for each zip file
            // extract to curr dir
            // delete the zip
        // set list of zips to new list of all .zip files in cur dir which will pick up any newly extracted zips
    // get list of all directories in cur dir (don't include sub directories)
    // for each dir in list
        // call extractZipsFromDir with iterated dir

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM