Alex Rivera | Logout

How does ZipInputStream.getNextEntry() work?

Asked 2012-08-02T19:03:44.257
20

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!

Edit
Report

2 Answers

31

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

answered 2012-08-02T19:19:07.027
5

Excerpt from: 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.

First, the order you add entries to the ZipOutputStream is the order they are physically located in the .zip file.

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.

So I would believe that you have to use the entries() method to see the order in which it will be iterated through.

 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());
    }
answered 2012-08-02T19:13:30.967

Your Answer