一个 SearchPattern 在 SevenZipSharp 中包含多个搜索模式
A SearchPattern contain multiple search pattern in SevenZipSharp
我使用 SevenZipSharp
来压缩我的文件和目录。
我使用下面的代码,效果很好:
var searchPattern = "*.txt";
compressor.CompressDirectory(directory, archiveName, password, searchPattern, recursion);
现在,我想通过更复杂的 SearchPattern 过滤目录文件,如下所示:
var searchPattern = "*.txt && *.xml";
compressor.CompressDirectory(directory, archiveName, password, searchPattern, recursion);
在这种情况下我得到:
Index was outside the bounds of the array
SearchPattern
有办法做到这一点吗?
如果不是,我该怎么做?
答案是否定的,你不能用 SearchPattern
来做到这一点。
源码中可以看到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
...
}
在内部,SevenZipSharp 调用 Directory.GetFiles,它不支持多个掩码。
所以你有几个选择:
- 一个一个压缩扩展名(第一次创建存档,然后将文件添加到目录)
- 创建另一个管理多个扩展(正则表达式?)的重载并为项目做出贡献
我使用 SevenZipSharp
来压缩我的文件和目录。
我使用下面的代码,效果很好:
var searchPattern = "*.txt";
compressor.CompressDirectory(directory, archiveName, password, searchPattern, recursion);
现在,我想通过更复杂的 SearchPattern 过滤目录文件,如下所示:
var searchPattern = "*.txt && *.xml";
compressor.CompressDirectory(directory, archiveName, password, searchPattern, recursion);
在这种情况下我得到:
Index was outside the bounds of the array
SearchPattern
有办法做到这一点吗?
如果不是,我该怎么做?
答案是否定的,你不能用 SearchPattern
来做到这一点。
源码中可以看到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
...
}
在内部,SevenZipSharp 调用 Directory.GetFiles,它不支持多个掩码。
所以你有几个选择:
- 一个一个压缩扩展名(第一次创建存档,然后将文件添加到目录)
- 创建另一个管理多个扩展(正则表达式?)的重载并为项目做出贡献