Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does ZipInputStream.getNextEntry() work?

Tags:

java

zip

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:

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

How would zis.getNextEntry() iterate through that?

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

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

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

Thanks!

like image 996
joshualan Avatar asked Aug 02 '12 19:08

joshualan


People also ask

What is Zipinputstream in Java?

This class implements an input stream filter for reading files in the ZIP file format. Includes support for both compressed and uncompressed entries.

How do I get files from ZipEntry?

To read the file represented by a ZipEntry you can obtain an InputStream from the ZipFile like this: ZipEntry zipEntry = zipFile. getEntry("dir/subdir/file1.


1 Answers

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

like image 64
Zoop Avatar answered Oct 01 '22 18:10

Zoop