C# - 解压目录及其子目录中的所有文件。

问题描述 投票:-1回答:1

我尝试将所有ZIP压缩包在各自的目录子目录下解压到各自的目录子目录下。

结构的例子。

-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

它应该是怎样的:

-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

我的尝试

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 
                        }
                    }
                }
            }
        }

这里有几个问题:

  1. 如何压下打开的 cmd.exe?
  2. 它甚至不迭代根目录下的ZIP存档。如何解决这个问题?
  3. 虽然7zip在 cmd.exe 打印一些提取的处理,什么都没有发生。如何解决这个问题?

先谢谢你了。

c# unzip 7zip
1个回答
-2
投票

试着写一个方法来提取一个zip,然后遍历其提取的内容,寻找并提取任何其他zip文件。你可以使用ZipFile、FileInfo和DirectoryInfo类。这里有一些伪代码...

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
© www.soinside.com 2019 - 2024. All rights reserved.