Java Requires Chilkat v11.0.0+
Chilkat2 Java
Iterate Over ZIP Entries Using EntryAt
This example demonstrates how to use the EntryAt method to iterate over all entries contained within a ZIP archive.
The EntryAt method retrieves a ZipEntry object for a given zero-based index.
This is useful for:
- Enumerating all entries in a ZIP archive
- Inspecting filenames, sizes, and timestamps
- Searching or filtering ZIP contents
Suppose the ZIP archive contains:
docs/readme.txt
images/logo.png
data/config.json The example loops through all ZIP entries and prints information about each entry.
Chilkat Java Downloads
import com.chilkatsoft.chilkat2.ChilkatException;
import com.chilkatsoft.chilkat2.Zip;
import com.chilkatsoft.chilkat2.ZipEntry;
public class ChilkatExample {
public static void main(String[] args) {
// Open an existing ZIP archive.
Zip zip = new Zip();
try {
zip.openZip("example.zip");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// Get the total number of entries in the ZIP archive.
int numEntries = zip.getNumEntries();
System.out.println("Number of ZIP entries = " + numEntries);
// Create a ZipEntry object that will be reused
// for each entry retrieved by EntryAt.
ZipEntry entry = new ZipEntry();
// Iterate over all ZIP entries.
int i = 0;
while (i < numEntries) {
// Retrieve the entry at index i.
try {
zip.entryAt(i, entry);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Entry " + i);
System.out.println(" FileName: " + entry.getFileName());
System.out.println(" Uncompressed Length: " + entry.getUncompressedLength());
System.out.println(" Compressed Length: " + entry.getCompressedLength());
System.out.println("");
i = i + 1;
}
// Sample output:
//
// Entry 0
// FileName: docs/readme.txt
// Uncompressed Length: 1204
// Compressed Length: 512
//
// Entry 1
// FileName: images/logo.png
// Uncompressed Length: 84211
// Compressed Length: 84102
//
zip.closeZip();
System.out.println("Done.");
}
}