Search code examples
c#sevenzipsharp

A SearchPattern contain multiple search pattern in SevenZipSharp


I use SevenZipSharp to compress my files and directories.

I use the following code and it works well:

var searchPattern = "*.txt";
compressor.CompressDirectory(directory, archiveName, password, searchPattern, recursion);

Now, i want filter the directory files by a more complicated SearchPattern like this:

var searchPattern = "*.txt && *.xml";
compressor.CompressDirectory(directory, archiveName, password, searchPattern, recursion);

In this case i get:

Index was outside the bounds of the array

Is there a way to do this by SearchPattern? If NO, How can i do this?


Solution

  • The answer is no, you can't do this with SearchPattern.


    As you can see in the source code here:

    public void CompressDirectory(
                string directory, Stream archiveStream,
                string password = "", string searchPattern = "*", bool recursion = true)
            {
            ...
    #if CS4
                files.AddRange((new DirectoryInfo(directory)).GetFiles(searchPattern).Select(fi => fi.FullName));
    #else
                foreach (FileInfo fi in (new DirectoryInfo(directory)).GetFiles(searchPattern))
                {
                    files.Add(fi.FullName);
                }
    #endif
            ...
            }
    

    Internally, SevenZipSharp calls Directory.GetFiles, which doesn't support multiple masks.


    So you have several alternatives:

    • Compress the extensions one by one (creating the archive the first time, and then adding the files to the directory)
    • Create another overload managing several extensions (regex?) and contribute to the project