0

I have MyFile.zip that has a main directory "MyMainFolder", and several SubDirectories inside of that, one of which I want to extract (MySubFolder)...with all of its subdirs and contents.

I am trying to figure out how to 'step-into' the MyMainFolder , so that I can extract 'MySubFolder'.

I have some code that will extract a folder as long as that folder I am looking for exists as the main folder in the zip...and I can detect if the main folder is called "MyMainFolder" so it knows to look inside that and extract from there rather than looking in the main zip root for MySubFolder).

using (ZipFile zip1 = ZipFile.Read(fileName))
{
    zipFile = ZipFile.Read(@""+fileName);
    var result = zipFile.Any(entry => entry.FileName.Contains("MySubFolder"));

   if (result == false)
   {
            MessageBox.Show("MyMainFolder detected....Extracting from MyMainFolder...");
            // something here that will extract JUST MySubFolder and contents
   } else {
    foreach (var e in selection)
   {             
           var selection = (from e in zip1.Entries where (e.FileName).Contains("NySubfolder") select e)               
           e.Extract(outputDirectory);        
   }
  }
}

So far, I have tried putting a separate using inside each part of the if-else, and I tried creating a seperate selectionX in which I tried to force the root-folder name (which will always be 'MyMainFolder' for this experiment) to be part of what it looked through, thinking I could then extract MySubFolder, but I couldn't get that to work either. I tried to incorporate several other methods I found on stackflow and elsehwere, like using parts of 'how to extract files, but ignoring the path in the zipfile' and other such posts to try and find a way to 'skip' over that main root folder when extracting. (so that it gets ONLY 'MySubFolder' (and contents) and extracts to outputDirectory (not MyMainFolder\MySubFolder...)

Any help is appreciated. Thanks!!

CopalFreak
  • 53
  • 1
  • 2
  • 11
  • Have you seen this? [Extracting a specific folder from a zip using DotNetZip](http://stackoverflow.com/questions/4788386/extracting-a-specific-folder-from-a-zip-using-dotnetzip?rq=1) – Alina B. Mar 28 '13 at 03:59
  • Yes..that is where I got the code above. I added the if-else. – CopalFreak Mar 28 '13 at 12:49
  • Still working on this one... I can 'detect' the existence of "MyMainFolder" if it's in the root of the zip file, but not the existence of "MySubFolder" that exists inside of 'MyMainFolder'... much less extract MySubFolder without extracting MyMainFolder. – CopalFreak Mar 30 '13 at 08:45
  • I have been told by Cheeso over at CodePlex that there is no way to do this directly, and that the best way around it is to enumerate through the entire contents until I come across what I am looking for and then extract that. I am pretty sure I have seen examples of ways to enumerate though a file and only extract what I need, so gonna try that. – CopalFreak Mar 30 '13 at 19:39

1 Answers1

1

Enumerating though the entire contents until I came across what I was looking for worked, but just as an experiment, I wanted to see if it could be done another way.

Since I was unable able to check the names of the subfolders inside a root folder, I figured I could just match what I was looking for as I was parsing through it, extracting only what I wanted to, and then just change the output path.

using (ZipFile zip1 = ZipFile.Read(fileName))
{
    zipFile = ZipFile.Read(@""+fileName);
    var result = zipFile.Any(entry => entry.FileName.Contains("MySubFolder"));

    if (result == false)
    {
        // something here that will extract JUST MySubFolder and content
        string TestX = Path.GetDirectoryName(e.FileName) ;
        string MyNewPath = outputDirectory+@"\"+TestX ;
        e.Extract(MyNewPath);
    } else {
        foreach (var e in selection)
   {             
        var selection = (from e in zip1.Entries where (e.FileName).Contains("MySubfolder")
        .select e)               
        e.Extract(outputDirectory);        
   }
}

Something like that.. Not very useful, but interesting and helped me learn a little. (if nothing else, an example of how NOT to do things..hehe) Thanks

CopalFreak
  • 53
  • 1
  • 2
  • 11