【发布时间】:2015-05-31 22:40:28
【问题描述】:
我有点陷入这个问题。我只想打印 zip 文件中的顶级目录。例如,我有一个具有以下结构的 zip 文件:
Sample.zip
- sound
- game
-start.wav
-end.wav
- Intro
- custom
- Scene
- fight
- Angle
..............
上图显示:Sample.zip有2个文件夹(sound和custom),sound里面有game和Intro等2个文件夹...
现在我知道如何打开并从 zip 文件中获取目录:例如(工作代码)
try {
appFile = ("../../Sample.zip"); // just the path to zip file
ZipFile zipFile = new ZipFile(appFile);
Enumeration<? extends ZipEntry> entries = zipFile.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
if(entry.isDirectory()){
String dir = entry.getName();
File file = new File(dir);
System.out.println(file.getParent());
}
}
} catch (IOException e) {
System.out.println("Error opening Zip" +e);
}
现在我也知道我可以使用.getParent()(如您在上面看到的)来获取顶级目录,但是上面的实现没有奏效。它会列出所有目录,比如
null
sound
game
null
custom
scene
Angle
我的问题是我如何才能只打印顶级文件夹,在上述情况下,sound 和 custom?
如有任何提示,我将不胜感激。
【问题讨论】:
-
@Ducan:我已经用什么不起作用(输出)更新了这个问题。
-
首先打印所有条目的名称。然后只打印目录条目的名称。现在阅读输出并尝试找出如何区分顶级目录条目和非顶级目录条目。