Extracting folder C#

Oct 6, 2010 at 10:58 AM

Im trying to create a small method that extract a folder from my zip file.
I would think that this is the right method, although i can't get it to work. 

Maybe its just me who is stupid and can't see the forest for the trees :S

        private void MyExtract()
        {
            using (ZipFile zip = ZipFile.Read(@"C:\zipname.zip"))
            {
                ZipEntry e = zip["foldername_in_zip"];
                e.Extract(@"C:\Somewhere\on\my\drive");
            }
        }

Coordinator
Oct 6, 2010 at 2:30 PM
Edited Oct 6, 2010 at 2:34 PM
Yes, I'm afraid it IS you. ;) There is no support in the zipfile format for a concept of object like "folder". You cannot operate on a "folder" that is contained in a zipfile in the same way you can operate on a folder in the Windows Explorer.

There is an entry type for a directory in the zip format, but the directory is not a container object. It contains no other files or sub-directories. If you want to access a directory from within a ZipFile, you should specify the name with a trailing slash. If you extract a ZipEntry that is of type directory (in other words the IsDirectory property is true), then DotNetZip will simply create the appropriate directory in your filesystem. It will not extra t any "contained" files, because as I said, there is no containing relationship in the zipfile.

I suppose I could add this capability to the library, but I haven't, not yet anyway.

I can think of 3 ways to extract all the files in a "folder" within a ZipFile: 1. you can use the ExtractSelectedFiles() method. 2. Use a LINQ query, and call Extract() on each of the selected entries. 3. iterate through all the entries, and compare the FileName property to the folder name you desire.

good luck.
Coordinator
Oct 6, 2010 at 2:32 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Oct 7, 2010 at 8:30 AM

Hi Cheeso,

I use the ExtractSelectedFiles() method, but the function doesn´t work recursive for  subdirectories, it is right???

To solve this problem I write this solution, but is there another way?

	ICollection<Ionic.Zip.ZipEntry> test = zip.SelectEntries("Applications\\*.*");
   	foreach (Ionic.Zip.ZipEntry file in test)
        {
        	file.Extract(tbTarget.Text);
        }
With kind regards
Origon
Coordinator
Oct 14, 2010 at 10:50 PM

I don't know what you mean by "is there another way?".   What is the problem you are trying to solve?  What's unsatisfactory about what you're doing now?

The file selection capability does support a filespec format to denote recursive selection:  You need to use a * as a directory, eg "Applications\\*\\*.*"  Check the documentation on the name criterion, in the FileSelector class, for more information.  The way you have done it, "Applications\\*.*", is not recursion.  This will select only files in the 1st level directory.  Any files in subdirectories will  not be selected (this is what I mean by "it's not recursion"). 

 

Oct 18, 2010 at 1:38 PM
Edited Oct 18, 2010 at 1:41 PM

Ok, i will try to explain the problem.

The method ExtractSelectedFiles() offers 5 possibilities to extraxt files. In my usecase I want extraxt all files from a folder and his subfolders (like the problem from cezor). The problem in the ExtractSelectedFiles() method is the following.

If I use the method only with the selectionCriteria it works fine, even recursive (means extract the folder, all subfolders and all files inside the folders)

 

ZipFile zip = ZipFile.Read("data.zip");
zip.ExtractSelectedEntries("Applications\\*.*");

 

If I want set a extract directory I have to use the 3. method invocation.

 

zip.ExtractSelectedEntries("Applications\\*.*", "Applications", ".\Result");

 

In this case the method extract only the files inside the folder "Applications". (not recursive).

I hope you understand my problem. I can´t extract an folder with all subfolders to a target directory. Therefore I use code like in my first post. 

 

"is there another way" means: does a method exist that only get a foldername (without other syntax like *.*) , targetfolder and bool (recursive or not)???

Cheeso: "What's unsatisfactory about what you're doing now?" --> 1. It´s to long, 2. no switch for recursion, 3. more than the foldername is necessary (*.*).

Cheeso: "The way you have done it, "Applications\\*.*", is not recursion." Why not??? I get all files from a folder and his subfolders.

Coordinator
Oct 24, 2010 at 5:02 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.