This project is read-only.

How to exclude a directory with DotNetZip?

May 4, 2009 at 12:23 AM
Edited May 4, 2009 at 12:25 AM
Hello! My name's Alessandro.
I'm using DotNetZip Beta and I love it. But I'd like to have a method (or a procedure) to exclude a directory from Zip method.

I've a directory "Pippo" with three subdirectories (no empty): "FolderOne", "FolderTwo" and "FolderThree". I need to Zip the "Pippo" directory but excluding the "FolderOne" directory, for example. For the files, I can use the searchCriteria in AddSelectedFiles method to exclude some files. How can I do this with a directory?

Many thanks!
May 4, 2009 at 4:54 PM
Alex, I believe you can do this with a filespec that matches the directory.

name != Pippo\FolderOne\*.*

What have you tried?
May 5, 2009 at 1:09 AM
I'm using Linux (and Mono 2.2) and this solution does not work (I wrote name != Pippo/FolderOne/*.* ). Tomorrow I will try on Windows too, but I think that in the string searchCriteria is possible specify only the filename without the path (full or partial).

Thank you however.
May 5, 2009 at 7:48 AM
Edited May 5, 2009 at 2:30 PM
Well, I tried on Windows too and it does not work.

EDIT: I downloaded the source code and in FileSelector.cs (at lines 540~544) I can read:
"///If the name pattern contains any slashes, it is
 /// matched to the entire filename, including the path; otherwise, it is matched
 /// against only the filename without the path.  This means a pattern of "*\*.*" matches
 /// all files one directory level deep
, while a pattern of "*.*" matches all files in
 /// all directories."

So, now I think it is a bug.


I've resolved my issue with this:

 for (int i = 0; i < ProfileToBackup.Count; i++)
                    string[] FolderToBackup = (string[])ProfileToBackup[i];
                    DirectoryInfo ProfileDirectory= new DirectoryInfo(FolderToBackup[1]);
                    FileInfo[] FileList = ProfileDirectory.GetFiles("*.*", SearchOption.AllDirectories);
                    foreach (FileInfo CurrentFile in FileList)
                        if (NameOfProgram == "OperaBrowser" && Environment.OSVersion.ToString().Contains("Unix") && CurrentFile.DirectoryName.Contains(Environment.GetFolderPath(Environment.SpecialFolder.Personal) + "/.opera/mail"))

                        else Zip.AddFile(CurrentFile.FullName, Path.GetDirectoryName(CurrentFile.FullName.Replace(FolderToBackup[1], FolderToBackup[0])));  

So, I can exclude the entire "mail" folder.
May 5, 2009 at 7:22 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
May 8, 2009 at 5:01 PM

Alessandro, I'm glad you solved your problem, but I am still trying to see if I can understand it.

When I use the code below, I get the expected results: excluding a directory from the files that are inserted into the zip.

	using (ZipFile zip = new ZipFile())
	    zip.AddSelectedFiles("name != Pippo\\two\\*.*", "Pippo", true);

All files in the Pippo directory are included in the zip, except files in the Pippo\two subdirectory. Is this what you want? It works for me with v1.8.3.1.  

May 8, 2009 at 9:05 PM

Yes! Now it works very well! Thank you very much!

Dec 23, 2009 at 6:35 AM
Edited Dec 23, 2009 at 6:36 AM

Hi Cheeso,

    I'm using the exclusion feature and its working perfectly except for when I try to exclude a file or folder with spaces. For example,

I'm trying to do this:


using (ZipFile zip = new ZipFile())
    zip.UpdateSelectedFiles("name != C:\\folder\\File 1", "C:\\Folder", "Folder", true);


but it seems to be getting confused by the space and throws this exception:

System.ArgumentException: '1'
at Ionic.FileSelector._ParseCriterion(String s)
at Ionic.FileSelector..ctor(String selectionCriteria)
at Ionic.Zip.ZipFile._AddOrUpdateSelectedFiles(String selectionCriteria, String directoryOnDisk, String directoryPathInArchive, Boolean recurseDirectories, Boolean wantUpdate)
at Ionic.Zip.ZipFile.UpdateSelectedFiles(String selectionCriteria, String directoryOnDisk, String directoryPathInArchive, Boolean recurseDirectories)

I tried putting quotes around the path like this: 

using (ZipFile zip = new ZipFile())
    zip.UpdateSelectedFiles("name != \"C:\\folder\\File 1\"", "C:\\Folder", "Folder", true);

but it threw the same exception. Any way around this?