Search code examples
c#dotnetzip

Searching for a file named "test (').txt"


I'm using dotnetzip in my app and I've got an issue report on a crash with files containing '(' and ')'

            using (ZipFile zip = new ZipFile("test.zip"))
            {
                var e = zip.SelectEntries("test (').txt"); //System.ArgumentException
                e = zip.SelectEntries("name = test (').txt"); //System.ArgumentException
                e = zip.SelectEntries("(name = test (').txt)"); //System.ArgumentException

                e = zip.SelectEntries( "'test ()'" ); // OK
                e = zip.SelectEntries( "'test (')'" ); //System.ArgumentException
                e = zip.SelectEntries( "test ()" ); //System.ArgumentException
            }

How can I select these files ?

zip.SelectEntries( "name = test (').txt)" )
'zip.SelectEntries( "name = test (').txt)" )' threw an exception of type 'System.ArgumentException'
Data: {System.Collections.ListDictionaryInternal}
HResult: -2147024809
HelpLink: null
InnerException: null
Message: "'name\u0006=\u0006test\u0006(''"
ParamName: null
Source: "Ionic.Zip"
StackTrace: "   at Ionic.FileSelector._ParseCriterion(String s)\r\n   at Ionic.FileSelector..ctor(String selectionCriteria, Boolean traverseDirectoryReparsePoints)\r\n   at Ionic.Zip.ZipFile.SelectEntries(String selectionCriteria)"
TargetSite: {Ionic.SelectionCriterion _ParseCriterion(System.String)}

Solution

  • Can't find a solution with SelectEntries.

    As suggested by Lasse V. Karlsen, I do the search outside the zip Library. As I'm only looking for a single file, this is straightforward:

    var e = zip.Entries.FirstOrDefault(s => s.FileName == "test (').txt");
    

    Note that if you have subfolders in zip, you must convert backslash to slash:

    string name=@"(=')\test (').txt"
    var e = zip.Entries.FirstOrDefault(s => s.FileName == name.Key.Replace(@"\", @"/"));