Sample code for 30+ languages & platforms
Java

Iterate Through ZIP Entries Using EntryAt and ZipEntry.GetNext

See more Zip Examples

This example demonstrates how to iterate through all entries in a ZIP archive using:

  • Zip.EntryAt to obtain the first entry
  • ZipEntry.GetNext to advance through the remaining entries

The example prints each ZIP entry's stored filename and whether the entry is a file or directory.

This approach is useful when sequentially processing ZIP entries without repeatedly calling EntryAt by index.

Suppose the ZIP archive contains:

docs/
docs/readme.txt
images/logo.png
hello.txt

The example iterates through each entry in the order stored within the ZIP archive.

Chilkat Java Downloads

Chilkat2 Java
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Zip;
import com.chilkatsoft.chilkat2.ZipEntry;

public class ChilkatExample {

    public static void main(String[] args) {
        boolean success = false;

        Zip zip = new Zip();

        // Open an existing ZIP archive.
        try {
            zip.openZip("c:/temp/example.zip");
        } catch (ChilkatException e) {
            System.out.println(e.getLastErrorText());
            return;
        }

        // A ZIP archive may contain zero entries.
        if (zip.getNumEntries() == 0) {
            System.out.println("The ZIP archive is empty.");

            zip.closeZip();
            return;
        }

        // ------------------------------------------------------------
        // Get the first ZIP entry.
        //
        // EntryAt(0,entry) initializes the ZipEntry object so that
        // it represents the first entry in the ZIP archive.
        //
        ZipEntry entry = new ZipEntry();

        try {
            zip.entryAt(0, entry);
            success = true;
        } catch (ChilkatException e) {
            success = false;
        }
        if (!success) {
            System.out.println(zip.getLastErrorText());
            return;
        }

        // ------------------------------------------------------------
        // Iterate through all ZIP entries.
        //
        // GetNext updates the same ZipEntry object so that it
        // represents the next entry in the ZIP archive.
        //
        while (success) {
            if (entry.getIsDirectory()) {
                System.out.println("[Directory] " + entry.getFileName());
            } else {
                System.out.println("[File] " + entry.getFileName());
            }

            // Advance to the next entry.
            success = entry.getNext();
        }

        zip.closeZip();

        System.out.println("Done.");
    }
}