如何判断“ZipArchiveEntry"是否存在是目录? [英] How to tell if a "ZipArchiveEntry" is directory?

查看:19
本文介绍了如何判断“ZipArchiveEntry"是否存在是目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 .Net 4.5.2 System.IO.Compression.ZipArchive 类处理一些 3rd 方 ZIP 文件,并且一切正常.
但是,我需要区分文件条目目录条目,最好不要先提取它们.

I am handling some 3rd party ZIP files with .Net 4.5.2 System.IO.Compression.ZipArchive class and all works nicely.
However, I need to tell apart file entries from directory entries, preferably without extracting them first.

有没有办法在不解压的情况下判断一个 ZipArchive 条目是否是一个目录?

据发现,ZipArchiveEntry 没有任何属性可以说明它是文件还是目录.

As far as could find out there is no property of ZipArchiveEntry that tells if it is a file or directory.

推荐答案

似乎我确实倾向于回答自己的问题.

Seems like I really do tend to answer my own questions.

无论如何,答案很简单:

Anyway, the answer is straight forward:

如果 ZipArchiveEntry 是一个目录,它的 FullName 属性将以/"结尾(例如 some_dir/")并且它的 Name 属性将为空字符串 ("").

If ZipArchiveEntry is a directory it will have its FullName property ending with "/" (e.g. "some_dir/") and its Name property will be empty string ("").

如果有人愿意将这些信息放在文档中......

If only someone cared to put that info in the documentation ...

哦,还有一个问题:
如果目录包含文件,则不必必须有一个单独的ZipArchiveEntry.
因此,如果您要查找特定目录,则不能只查找一个 ZipArchiveEntry,其中 Name 为空或 FullName 末尾带有/"> - 您必须解析整个树(每个 ZipArchiveEntryFullName).

Oh, and there is a catch:
If a directory contains files, there does not have to be a separate ZipArchiveEntry for it.
So, if you are looking for a specific directory you cannot just look for one ZipArchiveEntry with empty Name or with "/" at the end of FullName - you have to parse the whole tree (FullName for each ZipArchiveEntry).

幸运的是,我只需要检查所有条目是否有一个公共目录(所有 ZipArchiveEntry 项目的 FullName 属性应该以相同的字符串开头,格式为文件夹名称/").

Luckily I had only to check if there is a single common directory for all entries (FullName properties of all ZipArchiveEntry items shoud start with the same string in the form of "folder_name/").

这篇关于如何判断“ZipArchiveEntry"是否存在是目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆