Frage

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

War es hilfreich?

Lösung

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

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top