Java Requires Chilkat v11.0.0+
Chilkat2 Java
Find a ZIP Entry by EntryID Using EntryById
See more Zip Examples
This example demonstrates how to use the EntryById method to retrieve a ZIP entry using its unique EntryID.
Each ZipEntry object has an EntryID property that uniquely identifies the entry within the currently open ZIP object.
This is useful when:
- An application stores EntryID values for later use
- ZIP entries need to be retrieved without searching by filename
- Multiple entries may have similar names or paths
The example:
- Opens a ZIP archive
-
Retrieves an entry using
EntryAt -
Saves the entry's
EntryID -
Uses
EntryByIdto retrieve the same entry later
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;
}
// Retrieve the first entry in the ZIP archive.
ZipEntry entry = new ZipEntry();
try {
zip.entryAt(0, entry);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Original entry:");
System.out.println(" FileName: " + entry.getFileName());
System.out.println(" EntryID: " + entry.getEntryID());
System.out.println("");
// Save the EntryID for later use.
int entryId = entry.getEntryID();
// Create another ZipEntry object.
ZipEntry entry2 = new ZipEntry();
// Retrieve the same entry using EntryById.
try {
zip.entryById(entryId, entry2);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
System.out.println("Entry retrieved by EntryID:");
System.out.println(" FileName: " + entry2.getFileName());
System.out.println(" EntryID: " + entry2.getEntryID());
System.out.println("");
// The filenames and EntryID values should match.
zip.closeZip();
System.out.println("Done.");
}
}