Java
Chilkat2 Java
Iterate Through Matching ZIP Entries Using EntryMatching and ZipEntry.GetNextMatch
See more Zip Examples
This example demonstrates how to iterate through ZIP entries matching a wildcard pattern using:
-
Zip.EntryMatchingto obtain the first matching entry -
ZipEntry.GetNextMatchto advance to subsequent matching entries
The wildcard character * matches zero or more characters. Matching is performed against the full stored ZIP entry path.
The example searches for all entries beneath the docs/ directory.
Suppose the ZIP archive contains:
docs/
docs/readme.txt
docs/manual.pdf
docs/sub1/notes.txt
images/logo.png
hello.txt The wildcard pattern:
docs/* Matches:
docs/
docs/readme.txt
docs/manual.pdf
docs/sub1/notes.txt Note that ZIP archives may optionally contain separate directory entries. Therefore, the first matching entry may be the directory entry docs/ rather than a file 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) {
boolean success = false;
Zip zip = new Zip();
// Open an existing ZIP archive.
try {
zip.openZip("c:/temp/sample.zip");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
// ------------------------------------------------------------
// Find the first ZIP entry matching the wildcard pattern:
//
// docs/*
//
// Matching is performed against the full stored ZIP path.
//
ZipEntry entry = new ZipEntry();
try {
zip.entryMatching("docs/*", entry);
success = true;
} catch (ChilkatException e) {
success = false;
}
if (!success) {
System.out.println("No matching entries found.");
zip.closeZip();
return;
}
// ------------------------------------------------------------
// Iterate through all matching entries.
//
// GetNextMatch updates the same ZipEntry object so that
// it represents the next matching entry.
//
while (success) {
if (entry.getIsDirectory()) {
System.out.println("[Directory] " + entry.getFileName());
} else {
System.out.println("[File] " + entry.getFileName());
}
// Advance to the next matching entry.
success = entry.getNextMatch("docs/*");
}
zip.closeZip();
System.out.println("Done.");
}
}