18

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!

Martijn Courteaux
  • 65,802
  • 45
  • 192
  • 282
joshualan
  • 1,910
  • 6
  • 21
  • 31
  • ZipEntry can represent a directory too. See the [isDirectory()](http://docs.oracle.com/javase/6/docs/api/java/util/zip/ZipEntry.html#isDirectory()) method. – Bobulous Aug 02 '12 at 19:16

4 Answers4

25

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

Zoop
  • 596
  • 4
  • 7
16

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());
        }
    }
}
Community
  • 1
  • 1
Manisha Mahawar
  • 601
  • 6
  • 9
  • I am getting a zip file that has pages of document named as `[filename]-[page-number].jpg`. After getting zip file, I will make it read in sequence, and I am expecting to get files from 1 to last page. However, I see sometimes it confuses the order of files. For example, pages are taken in this order: 1, 2, 4, 3, 5, 6, 7, 9, 8, 10. Why do this happen? – efirat Mar 26 '18 at 07:15
  • Every record in zip has its "offset" from beginning. getNextEntry return items in order based on this offset (starts with offset 0). Some software (like [7zip](https://www.7-zip.org/)) show this offset when you open zip file... – Peter Spireng Jul 13 '18 at 05:10
1

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());
    }
Vikram
  • 4,074
  • 8
  • 41
  • 63
1

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.

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).

Hot Licks
  • 46,205
  • 16
  • 90
  • 149