This project is read-only.

Using - how do you exclude a file using the zip.AddDirectory?

Mar 6, 2009 at 10:50 PM
For example, how can you exclude "*.pst" files?   I was thinking there would be a zip.addDirectory(source, filestoexclude) kind of thing..
Mar 6, 2009 at 10:51 PM
The current method signature is:

ZipFile.AddSelectedFiles("name != *.pst", MyDirectory, True)

True  means "recurse directories".

Does it make sense to reverse the Directory and the selection criteria string?  
Does it make sense to default the recurse directories behavior to "true" ?
Mar 7, 2009 at 7:17 AM
Hi Cheeso,

Yes it make sense to reverse source and selection, for the recursive bool is less evident.
Mar 8, 2009 at 8:33 PM
Edited Mar 8, 2009 at 8:35 PM
Hey Dom, thanks for the feedback.  I'm looking at that method, and I feel like it could go either way.

To keep similarity with the existing AddDirectory() method, the order of params should be AddSelectedFiles(directoryPath, selectionCriteria).
To keep similarity with the AddSelectedFiles() method, the order of params should be AddSelectedFiles(selectionCriteria, directoryPath).

To address this, I may add a new overload for AddDirectory(), and specify 
AddDirectory(directoryPath, selectionCriteria).  (with recursion always true).
But the problem there is, there is already an AddDirectory(String, String) = the second string is the directory path to use in the archive.
Mar 9, 2009 at 3:12 PM
Using -
I tried the ZipFile.AddSelectedFiles("name != *.pst", SourceDirectory, True) but it did not zip all the files or subdirectories within the SourceDirectory.
It did not error, just my expectation was that it would zip all files and subdirectories as before, yet exclude any .pst files.
You mentioned above you might try to add a new overload to AddDirectory. When you do, I'll be happy to test.
Question though, when you say selectionCriteria, does that mean to include or exclude? Or both depending on syntax?
Mar 9, 2009 at 7:01 PM
On the AddSelectedFiles thing -

AddSelectedFiles("name != *.pst", SourceDir, True) is expected to add all the files in SourceDir or any child directory, that are  not pst files (not ending in .pst as an extension) into the zip file. 
You said it didn't behave as you expected.  What did it do?  Which files or folders were not included, that you thought should be included?

I found a glitch in the FileSelector, and fixed it, just this morning.  but I'm not sure it's the same problem you are running into here.
The problem I found was that the match was failing if a filename contained more than one dot.  Like Form1.Designer.vb would not match "*.*"    I've fixed that, I think.
What did YOU see?

I also noticed a problem in the example code in the doc - I had omitted a call to zip.Save after adding the files!!  whoops.

Mar 9, 2009 at 7:21 PM
I basically took this code in my program:
            zipArchive.CompressionLevel = Zlib.CompressionLevel.LEVEL9_BEST_COMPRESSION
            zipArchive.UseZip64WhenSaving = Ionic.Zip.Zip64Option.Always
            zipArchive.TempFileFolder = txtFolderSave.Text

Change it to:
            zipArchive.AddSelectedFiles("name != *.pst", SourceDir, True)
            zipArchive.CompressionLevel = Zlib.CompressionLevel.LEVEL9_BEST_COMPRESSION
            zipArchive.UseZip64WhenSaving = Ionic.Zip.Zip64Option.Always
            zipArchive.TempFileFolder = txtFolderSave.Text

It zipped with out error, but when I opened the zip file there were a lot of files/subdirectories missing. So many I just reverted back to the AddDirectory method (because I was working on other stuff - didn't have time to dig deeper).
Example directory folder structure (assume many more sub dirs with files in each)-
My Docs
My Docs\ABC
My Docs\XYZ
My Docs\VB2005

After zipping, the one folder in particular I noticed was the VB2005 folder (which contains 8 subdirs under VB2005 (many many more folders and file under those 8)); nothing was there. The VB2005 folder was there, but nothing else - 1 file I think. This might not help you much I know, so I'll try to test more soon.
But that's what happened.

Mar 9, 2009 at 8:39 PM
Thank you, I will look further.
Mar 9, 2009 at 9:03 PM
Jeff, you found a bug.
I just fixed it.   It was a one-line thing.  The code was ignoring the recurseDirectories parameter.
v1.8.1.19 has the correction.  Please try it out.
let me know.
Mar 16, 2009 at 3:34 PM
Sorry for the delay in this test...
It worked fine and recursed thru the directories.
Couple of questions though when you have a moment.
When zipping the My Documents directory, upon opening the zip archive, I noticed it contained the whole path from the root. In this case, Users\jeffy\Documents\. The AddDirectory doesn't do this, I assume because of the nature it which it recurses thru the directory.

Using AddSelectedFiles("name != *.pst", SourceDir, True):
Is there a way to eliminate the preceding path to the directory being zipped?
Is there a way to exclude more than one file? In example (and i'm guessing): AddSelectedFiles("name != *.pst | name != *.doc", SourceDir, True)
If there were no exclusions, could I keep the same method syntax and put in some sort of null for the exclusion parameter?

I want to build in some sort of selectable exclusions GUI for the user. So I need to code it in such a way, roughly speaking, that it is like:

Dim selection1 as string 'user exclusion selection 1
Dim selection2 as string 'user exclusion selection 2
Dim exclusion as string="name != " & selection1
AddSelectedFiles(exclusion, SourceDir, True)

Where exclusion could be null, could have 1 string value, 2 values, etc.
That is real rough code just shooting from the hip (I haven't done anything yet), but you get the idea (I hope).

Your thoughts?

Mar 22, 2009 at 6:32 AM

Hi Jeff!

About compound selection criteria -
You would use something like this:   (name != *.pst  AND  name != *.doc)
I tried to document this in the help file.

About the whole path from the root -
There is an overload of ZipFile.AddSelectedFiles() that allows you to specify the directory in the archive. 

With the overload, you can, for example, do

  ZipFile.AddSelectedFiles("name != *.pst", "c:\My Docs\Local", "archive", true) 

This will add all the files in c:\my docs\local into the zip archive, but will use "archive" as the root directory in the archive. You will have to test it out to see what I mean. 

One last thing:: thanks for confirming that the changes worked as advertised.  If you have NEW questions, I'd appreciate it if you could open a new thread. It makes it nicer for search and it makes it easier for other people to benefit from the Q&A.

Mar 29, 2009 at 6:32 PM
Hello, my name's Alessandro.
I'm using DotNetZip but I have some problems with the AddSelectedFiles Method when I try to zip a folder with the necessity of exclusion some files.
For example, I tried to zip the Mozilla Profile Folder:

ZipFile _zip = new ZipFile();
_zip.AddSelectedFiles("name != parent.lock and name != places.sqlite-journal", _profilePath, "Mozilla", true);

It makes the zip file but the files contained in the zip file lose the tree of the original folder.

I found this problem with the AddFile Method too.

This is my code:
string[] _tempArray = Directory.GetFiles(_profilePath, "*.*", SearchOption.AllDirectories);
int i = -1;
 foreach (string _entryToAdd in _tempArray)
    if (!(_entryToAdd.Contains("parent.lock")) & !(_entryToAdd.Contains("places.sqlite-journal")))


Same problem: lose the tree of original folder :-(
I tried with 1.7 branch too.
Can you help me?
Many thanks!

Mar 29, 2009 at 7:50 PM
Edited Mar 29, 2009 at 8:23 PM
The behavior with AddSelectedFiles sure looks like a basic bug. I've opened a workitem.

The behavior with AddFile is expected and documented.  With AddFile(), you are specifying the name of a file on disk, and the name of a directory in the archive.  The name of the dir in the archive is always "Mozilla", which means you "flatten" the directory hierarchy.  Using AddFile(), to reproduce the hierarchy of the directory tree on disk, in the zip archive, you would have to append the dir path to "Mozilla".  eg something like

_zip.AddFile(_nameOfFileToAdd, Path.Combine("Mozilla",Path.GetDirectoryName(_nameOfFileToAdd.Replace(_profilePath, ""))));
You can also get the directory tree if you call the AddFiles() method:

        Ionic.FileSelector ff = new Ionic.FileSelector(selectionCriteria);
        var filesToAdd = ff.SelectFiles(_directoryToZip, recurseDirectories);
        using (ZipFile _zip = new ZipFile())
But, this does not allow you to specify the top-level directory to use in the zip, a la "Mozilla".
Mar 29, 2009 at 7:51 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Mar 29, 2009 at 9:15 PM
Alessandro, the problem is fixed in v1.8.2.5.
Mar 29, 2009 at 9:38 PM
I tried in this moment but now there is another problem with the AddSelectedFiles method: in the zip file there is the complete tree path of the original folder.
In my case "Users\Administrator\AppData\Roaming\Mozilla\".
It seems that the string directoryPathInArchve value passed to the method is not working. :-(
Mar 29, 2009 at 9:40 PM
Thanks for your work however ;-)
I hope you fix this problem in the future.
Mar 29, 2009 at 10:28 PM
Yes, I see.  Thanks for working through this with me. 
Can you try v1.8.2.7 ?

Mar 29, 2009 at 10:44 PM
Well done, Cheeso! Now it's working!
Many thanks!

Mar 29, 2009 at 10:48 PM
The version has been fixed this bug ;-)
Mar 29, 2009 at 11:32 PM
Thanks, Alessandro, for reporting it and working through it with me.