Possible Bug with AddSelectedFiles

Feb 3, 2010 at 6:09 PM

Hello.  I'm having an issue with the ZipFile.AddSelectedFiles function.    If the files I'm zipping contain a "~" and the directoryOnDisk I provide does not match the exact case of the path, I will get the full path in the zip archive, even when I specify an empty string for directoryPathInArchive.  Let me give an example.

Let's say I have the following file "5.txt" in my folder "C:\\Test".  I want to zip this to somewhere on my harddrive.  I do the following.

ZipFile z = new ZipFile();
z.AddSelectedFiles("name != *.zip*", "C:\\Test\\", "", true);

Works as expected.  My zip archive contains "5.txt" and nothing else.

z.AddSelectedFiles("name != *.zip*", "C:\\test\\", "", true);

Also works even though "test" was not cased correctly to match the actual directory.  Now I rename "5.txt" to "~5.txt" and repeat the above line.

The result is a zipfile containing Test\~.5.txt.  The full path is present.  Now if I correctly type the directory such as:

z.AddSelectedFiles("name != *.zip*", "C:\\Test\\", "", true);

It works for "~" files.  I took a look at the source code and I may have found the issue.  On line 558 of ZipFile.Selector is the following:

string dirInArchive = Path.GetDirectoryName(f).Replace(directoryOnDisk, directoryPathInArchive);

It looks through each file and does a replace.  Path.GetDirectoryName however doesn't seem to return the correct path if the file contains a "~' and the case does not match the directory path.  Perhaps this may fix it?

string dirInArchive = Path.GetDirectoryName(f).ToLower().Replace(directoryOnDisk, directoryPathInArchive);

I'm not sure if this makes any sense at all but any input would be helpful.  I can send a sample project if necessary.

Feb 3, 2010 at 6:18 PM

Sorry my fix suggestion is wrong haha.  This instead.

string dirInArchive = Path.GetDirectoryName(f).ToLower().Replace(directoryOnDisk.ToLower(), directoryPathInArchive);

Although the .ToLower() for directoryOnDisk could be done before the loop.

Feb 3, 2010 at 8:09 PM
This discussion has been copied to a work item. Click here to go to the work item and continue the discussion.
Feb 11, 2010 at 3:53 AM

Arbel, thanks for the analysis and the bug report. Very helpful .

I've put the fix in; it will be available in v1.9.1.4, or v1.9.5.2. 

Feb 12, 2010 at 3:21 AM

v1.9.1.4 is now available.  It includes this fix.


Feb 12, 2010 at 2:39 PM