C# 存档中的文件列表
我正在创建一个 FileFinder 类,您可以在其中进行如下搜索:
var fileFinder = new FileFinder(
new string[]
{
"C:\\MyFolder1",
"C:\\MyFolder2"
},
new string[]
{
"*.txt",
"*.doc"
} );
fileFinder.FileFound += new EventHandler<FileFinderEventArgs>(FileFinder_FileFound);
DoSearch();
如果我执行该代码,则每次 *.txt
或 时都会调用
文件。FileFinder_FileFound
在 C:\\MyFolder1
及其子文件夹或 C:\\MyFolder2
及其子文件夹中找到 *.doc
因此,该类会查看子文件夹,但我也希望它能够查看它遇到的任何 zip 文件,就像它们是文件夹一样。我该怎么做?最好不要创建临时文件...
编辑忘记提及这不是个人项目;它用于我正在与我的公司合作的商业应用程序。
I am creating a FileFinder class, where you can do a search like this:
var fileFinder = new FileFinder(
new string[]
{
"C:\\MyFolder1",
"C:\\MyFolder2"
},
new string[]
{
"*.txt",
"*.doc"
} );
fileFinder.FileFound += new EventHandler<FileFinderEventArgs>(FileFinder_FileFound);
DoSearch();
If I executed that code, FileFinder_FileFound
would be called every time a *.txt
or *.doc
file was found in C:\\MyFolder1
and its subfolders, or C:\\MyFolder2
and its subfolders.
Sot the class looks through subfolders, but I also want it to look through any zip files it comes across, as if they were folders. How can I do this? It would be preferable that no temporary files be created...
EDIT Forgot to mention this is not a personal project; its for a commercial application I'm working on with my company.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您可以使用 .NET 4.5 或更高版本,那么您现在终于可以使用 ZipArchive。它位于 System.IO.Compression 命名空间中。该示例也使用了 ZipFile 类,它不仅需要引用 System.IO.Compression 程序集,还需要引用 System.IO.Compression.FileSystem 程序集(两者都贡献于相同的名称空间)。
MSDN: http:// /msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx
因此,如果您的查找器遇到 zip 文件,您可以执行以下操作(其要点):
If you can use .NET 4.5 or higher you can now finally use
ZipArchive
. It's in theSystem.IO.Compression
namespace. The example usesZipFile
class too, which requires not just referencingSystem.IO.Compression
assembly, butSystem.IO.Compression.FileSystem
assembly too (both contribute to the same namespace).MSDN: http://msdn.microsoft.com/en-us/library/system.io.compression.ziparchive%28v=vs.110%29.aspx
So if your finder encounter a zip file, you can do something like this (the gist of it):
查看 . NET 3.5 及更高版本。您将在此处找到一些很好的答案
Checkout System.IO.Packaging Namespace available in .NET 3.5 and higher. You'll find some good answers here
您应该使用像 SharpZipLib 这样的工具: http://www.icsharpcode.net/opensource/sharpziplib/。这允许您列出 .zip 文件中的文件,并可选择提取它们。
.NET 框架本身不支持使用
FileFinder
在 .zip 文件中搜索。You should use a tool like SharpZipLib: http://www.icsharpcode.net/opensource/sharpziplib/. This allows you to list the files in a .zip file and, optionally extract them.
The .NET framework does not natively support using the
FileFinder
to search in .zip files.