java ZipInputStream.getNextEntry() 如何工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11784102/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How does ZipInputStream.getNextEntry() work?
提问by joshualan
Say we have code like:
假设我们有这样的代码:
File file = new File("zip1.zip");
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
Let's assume you have a .zip file that contains the following:
假设您有一个包含以下内容的 .zip 文件:
- zip1.zip
- hello.c
- world.java
- folder1
- foo.c
- bar.java
- foobar.c
- zip1.zip
- 你好ç
- 世界.java
- 文件夹 1
- foo.c
- 酒吧.java
- foobar.c
How would zis.getNextEntry() iterate through that?
zis.getNextEntry() 将如何遍历它?
Would it return hello.c, world.java, folder1, foobar.c and completely ignore the files in folder1?
它会返回 hello.c、world.java、folder1、foobar.c 并完全忽略 folder1 中的文件吗?
Or would it return hello.c, world.java, folder1, foo.c, bar.java, and then foobar.c?
或者它会返回 hello.c、world.java、folder1、foo.c、bar.java,然后是 foobar.c?
Would it even return folder1 since it's technically a folder and not a file?
它甚至会返回folder1,因为它在技术上是一个文件夹而不是一个文件?
Thanks!
谢谢!
回答by Zoop
Well... Lets see:
好吧,走着瞧:
ZipInputStream zis = new ZipInputStream(new FileInputStream("C:\New Folder.zip"));
try
{
ZipEntry temp = null;
while ( (temp = zis.getNextEntry()) != null )
{
System.out.println( temp.getName());
}
}
Output:
输出:
New Folder/
New Folder/folder1/
New Folder/folder1/bar.java
New Folder/folder1/foo.c
New Folder/foobar.c
New Folder/hello.c
New Folder/world.java
新建文件夹/
新建文件夹/folder1/
新建文件夹/folder1/bar.java
新建文件夹/folder1/foo.c
新建文件夹/foobar.c
新建文件夹/hello.c
新建文件夹/world.java
回答by Manisha Mahawar
Yes. It will print the folder name too, since it's also an entry within the zip. It will also print in the same order as it is displayed inside the zip. You can use below test to verify your output.
是的。它也会打印文件夹名称,因为它也是 zip 中的一个条目。它也将按照在 zip 中显示的相同顺序打印。您可以使用以下测试来验证您的输出。
public class TestZipOrder {
@Test
public void testZipOrder() throws Exception {
File file = new File("/Project/test.zip");
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));
ZipEntry entry = null;
while ( (entry = zis.getNextEntry()) != null ) {
System.out.println( entry.getName());
}
}
}
回答by Vikram
Excerpt from: https://blogs.oracle.com/CoreJavaTechTips/entry/creating_zip_and_jar_files
摘自:https: //blogs.oracle.com/CoreJavaTechTips/entry/creating_zip_and_jar_files
java.util.zip libraries offer some level of control for the added entries of the ZipOutputStream.
java.util.zip 库为 ZipOutputStream 的添加条目提供了某种程度的控制。
First, the order you add entries to the ZipOutputStream is the order they are physically located in the .zip file.
首先,您向 ZipOutputStream 添加条目的顺序是它们在 .zip 文件中的物理位置顺序。
You can manipulate the enumeration of entries returned back by the entries() method of ZipFile to produce a list in alphabetical or size order, but the entries are still stored in the order they were written to the output stream.
您可以操作由 ZipFile 的 entries() 方法返回的条目的枚举,以按字母顺序或大小顺序生成一个列表,但这些条目仍按它们写入输出流的顺序存储。
So I would believe that you have to use the entries() method to see the order in which it will be iterated through.
所以我相信你必须使用 entry() 方法来查看它将被迭代的顺序。
ZipFile zf = new ZipFile("your file path with file name");
for (Enumeration<? extends ZipEntry> e = zf.entries();
e.hasMoreElements();) {
System.out.println(e.nextElement().getName());
}
回答by Hot Licks
The zip file internal directory is a "flat" list of all the files and directories in the zip. getNextEntry
will iterate through the list and sequentially identify every file and directory in the zip file.
zip 文件内部目录是 zip 中所有文件和目录的“平面”列表。 getNextEntry
将遍历列表并按顺序识别 zip 文件中的每个文件和目录。
There is a variant of the zip file format that has no central directory, in which case (if it's handled at all) I suspect you'd iterate through all actual files in the zip, skipping directories (but not skipping files in directories).
有一种没有中央目录的 zip 文件格式的变体,在这种情况下(如果它被处理的话)我怀疑你会遍历 zip 中的所有实际文件,跳过目录(但不跳过目录中的文件)。