Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/search/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#存档中的文件列表_C#_Search_Zip - Fatal编程技术网

C#存档中的文件列表

C#存档中的文件列表,c#,search,zip,C#,Search,Zip,我正在创建一个FileFinder类,您可以在其中执行如下搜索: var fileFinder = new FileFinder( new string[] { "C:\\MyFolder1", "C:\\MyFolder2"

我正在创建一个FileFinder类,您可以在其中执行如下搜索:

    var fileFinder = new FileFinder(
                         new string[]
                             {
                                  "C:\\MyFolder1",
                                  "C:\\MyFolder2" 
                             }, 
                         new string[]
                             {
                                  "*.txt",
                                  "*.doc"
                             } );
    fileFinder.FileFound += new EventHandler<FileFinderEventArgs>(FileFinder_FileFound);
    DoSearch();
var fileFinder=newfilefinder(
新字符串[]
{
“C:\\MyFolder1”,
“C:\\MyFolder2”
}, 
新字符串[]
{
“*.txt”,
“*.doc”
} );
fileFinder.FileFound+=新事件处理程序(fileFinder\u FileFound);
DoSearch();
如果我执行该代码,每次在
C:\\MyFolder1
及其子文件夹或
C:\\MyFolder2
及其子文件夹中找到
*.txt
*.doc
文件时,都会调用
FileFinder\u filefind

所以这个类会查看子文件夹,但我也希望它能查看它遇到的任何zip文件,就像它们是文件夹一样。我该怎么做?最好不要创建临时文件


编辑忘记提及这不是个人项目;这是我与公司合作开发的商业应用程序。

您应该使用SharpZipLib:之类的工具。这允许您在.zip文件中列出文件,并可以选择提取它们


.NET framework本机不支持使用
FileFinder
在.zip文件中搜索。

签出可在.NET 3.5及更高版本中获得。如果你能使用.NET 4.5或更高版本,你会找到一些好的答案,现在你终于可以使用ZipArchive了。它位于
System.IO.Compression
名称空间中。该示例还使用了
ZipFile
类,这不仅需要引用
System.IO.Compression
assembly,还需要引用
System.IO.Compression.FileSystem
assembly(两者都有助于相同的命名空间)。 MSDN:

因此,如果您的查找程序遇到zip文件,您可以执行以下操作(要点):


ZipPackage假定zip根文件夹中存在[Content_types].xml,这可能不适合一般情况。但它适用于MS自己的格式,如xlsx、docx、XPS、nupkg、visx,或者如果您是使用ZipPackage创建zip文件的人。
using System;
using System.IO;
using System.IO.Compression;

using (ZipArchive archive = ZipFile.OpenRead(zipFilePath))
{
    foreach (ZipArchiveEntry entry in archive.Entries)
    {
        if (entry.FullName.EndsWith(".txt", StringComparison.OrdinalIgnoreCase) ||
            entry.FullName.EndsWith(".doc", StringComparison.OrdinalIgnoreCase))
        {
            // FileFinder_FileFound(new FileFinderEventArgs(...))
        }
    }
}