Search code examples
c#dotnetzip

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


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?


Solution

  • 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).