简体   繁体   English

ZipEntry.isDirectory()和枚举 <? extends ZipEntry> 仅查看目录内容,而不查看目录

[英]ZipEntry.isDirectory() and Enumeration<? extends ZipEntry> only see directory contents, not the directory

I'm working on looking through a .zip file to confirm that all the contained files are named correctly, and running into some trouble. 我正在研究.zip文件,以确认所有包含的文件都正确命名,并且遇到了一些麻烦。 Here's the file hierarchy: 这是文件层次结构:

-.zip
  -dir
    -file1
    -file2
    -file3
  -file4
  -file5
  -file6

Code: 码:

for (Enumeration<? extends ZipEntry> e = zf.entries(); e.hasMoreElements(); ) {
            ZipEntry entry = e.nextElement();
            System.out.println("ZipEntry name: " + entry.getName());
            ...
}

Usual output: 通常的输出:

ZipEntry name: dir/file1
ZipEntry name: dir/file2
ZipEntry name: dir/file3
ZipEntry name: file4
ZipEntry name: file5
ZipEntry name: file6

The desired output would also contain 所需的输出还将包含

ZipEntry name: dir/

Strangely enough, I have one test case where I get the desired output: when using a .zip of identical hierarchy that was emailed to me. 奇怪的是,我有一个测试用例,可以得到所需的输出:使用通过电子邮件发送给我的具有相同层次结构的.zip时。 Whenever I use the .zips that I have compressed on my machine, I get the "Usual output." 每当我使用在计算机上压缩的.zip时,都会得到“常规输出”。 I'd really like to have consistent behavior, but I can't figure out what's going wrong. 我真的很想保持一致的行为,但是我无法弄清楚出了什么问题。 Any ideas? 有任何想法吗?

Thanks in advance! 提前致谢!

There aren't really directories in a zip file. 压缩文件中实际上没有目录。 There are entries. 有条目。 And ZipEntry#isDirectory() checks if the name of the entry ends with a / to determine if it should be considered a directory. ZipEntry#isDirectory()检查条目名称是否以/结尾,以确定是否应将其视为目录。 So what your zip file contains is 所以您的zip文件包含的是

dir/file1
dir/file2
dir/file3
file4
file5
file6

For that strange case, the zip probably actually contains 对于这种奇怪的情况,该zip实际上可能包含

dir/   // the entry won't actually have any content though
dir/file1
dir/file2
dir/file3
file4
file5
file6

This is useful for zip file managers to determine how entries should be extracted. 这对于zip文件管理器确定应如何提取条目非常有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM