ZipInputStream.getNextEntry()如何工作? [英] How does ZipInputStream.getNextEntry() work?

查看:3389
本文介绍了ZipInputStream.getNextEntry()如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我们有以下代码:

File file = new File("zip1.zip");
ZipInputStream zis = new ZipInputStream(new FileInputStream(file));

我们假设您有一个包含以下内容的.zip文件:

Let's assume you have a .zip file that contains the following:


  • zip1.zip

    • hello.c

    • world.java

    • folder1

      • foo.c

      • bar.java

      • zip1.zip
        • hello.c
        • world.java
        • folder1
          • foo.c
          • bar.java

          zis.getNextEntry()将如何迭代?

          How would zis.getNextEntry() iterate through that?

          它会返回hello.c,world.java,folder1,foobar.c并完全忽略folder1中的文件吗?

          Would it return hello.c, world.java, folder1, foobar.c and completely ignore the files in folder1?

          或者它会返回hello.c,world.java,folder1,foo.c,bar.java,然后是foobar.c吗?

          Or would it return hello.c, world.java, folder1, foo.c, bar.java, and then foobar.c?

          它会不会返回folder1,因为它在技术上是一个文件夹而不是文件?

          Would it even return folder1 since it's technically a folder and not a file?

          谢谢!

          推荐答案

          是。它也会打印文件夹名称,因为它也是zip中的条目。它的打印顺序与zip中显示的顺序相同。您可以使用以下测试来验证您的输出。

          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.

          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());
                  }
              }
          }
          

          这篇关于ZipInputStream.getNextEntry()如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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