DotNetZip | C# Getting the extracted folder, not the .zip file that has just been unzipped?

StackOverflow https://stackoverflow.com/questions/18882957

  •  29-06-2022
  •  | 
  •  

Frage

I currently have this code:

     foreach (string file in openFileDialog1.FileNames)
     {
     String ExtractPath = Path.GetDirectoryName(file);   
         try
         {
             using (ZipFile zip = ZipFile.Read(file))
             {
                 zip.ExtractProgress += ExtractProgress;
                 foreach (ZipEntry e in zip)
                 {
                     try
                     {                             
                         e.Extract(ExtractPath,ExtractExistingFileAction.OverwriteSilently);  // true => overwrite existing files
                      }
                      catch
                      {
                      }
                    }
                 }
            }
            catch
            {
            }
       }

This works fine and extracts one or more selected zip files at the same time. But I'm confused about how I should go about creating a separate directory for each file and placing each file into the created directory.

Example:

User selects 2 zip files to extract. The 2 zip files are called "A.zip" and "B.zip"

I would like to programatically put the extracted files into their own directory so I can sift through them for further use.

So the zip file "A.zip" would extracted and the files extracted would be put into a folder called "Unzipped A" and the zip file "B.zip" would be extracted and the file extracted would put into a folder called "Unzipped B".

I'm sorry if this is confusing. Help would be greatly appreciated.

Okay so after using and editing MatteKarla's snippets I now have this:

            foreach (string file in openFileDialog1.FileNames)
        {
            string directory = Path.GetDirectoryName(file) + @"\Unzipped " + Path.GetFileNameWithoutExtension(file);
            var GetFiles = Directory.GetFiles(directory, "*.txt", SearchOption.AllDirectories).Where(s => s.EndsWith(".txt"));
            foreach (string text in GetFiles)
            {
                MessageBox.Show("Text found", "File");
            }
        }

This searches the extracted files in my created directory for a .txt file and it works perfectly, I was just wondering if this is the proper way to do it or is there a more efficient way?

War es hilfreich?

Lösung

Using the filename without extension you could combine the path and filename without extension using Path.GetFileNameWithoutExtension and Path.Combine to create a new path.

Finally just create that directory with Directory.CreateDirectory so the directory exists when you try to extract the zip-file to your ExtractPath.

This will unpack all zip-files to the directory where the zip-file being unpacked is, usually all files are in same directory when using OpenFileDialog.

foreach (string file in openFileDialog1.FileNames)
 {
     String ExtractPath = Path.GetDirectoryName(file);   
     string directory = "Unzipped " + Path.GetFileNameWithoutExtension(file);
     ExtractPath = Path.Combine(ExtractPath, directory);
     Directory.CreateDirectory(ExtractPath);

     try
     {string ExtractPath = Path.GetDirectoryName(file);

If you want all files to be extracted to a specific directory then set the directory outside of the for loop and just combine that path with "Unzipped " + filename.

String unpackPath = @"C:\UnpackPath";  
foreach (string file in openFileDialog1.FileNames)
 {
     string directory = "Unzipped " + Path.GetFileNameWithoutExtension(file);
     string ExtractPath = Path.Combine(unpackPath, directory);
     Directory.CreateDirectory(ExtractPath);

This will create unpack the files in folders like: "C:\UnpackPath\Unzipped A", "C:\UnpackPath\Unzipped B" (using your example file names).

Andere Tipps

You can use the .Net's

Directory.Create("url") ;

and also

Path.GetFileName("url"); // Separates the file name from the path

to both create the directories and get the file names which need to be stored in there.

You can dynamically create the directories by for example passing the zipped file name to directory + a valid path like

GetCurrentDirectory () + zipped file name 
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top