Java Requires Chilkat v11.0.0+
Chilkat2 Java
Unzip Selected Files from a Zip Archive
See more Zip Examples
Demonstrates how to iterate over the files contained within a .zip and unzip specific files.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) {
// This example requires the Chilkat API to have been previously unlocked.
// See Global Unlock Sample for sample code.
Zip zip = new Zip();
try {
zip.openZip("my_files.zip");
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
String unzipDir = "/temp/unzipDir";
// Get the number of files and directories in the .zip
int n = zip.getNumEntries();
ZipEntry entry = new ZipEntry();
int i = 0;
while (i < n) {
zip.entryAt(i, entry);
if (!entry.getIsDirectory()) {
// (the filename may include a path)
System.out.println(entry.getFileName());
// Your application may choose to unzip this entry
// based on the filename.
// If the entry should be unzipped, then call Extract(unzipDir)
try {
entry.extract(unzipDir);
} catch (ChilkatException e) {
System.out.println(e.getLastErrorText());
return;
}
}
i = i + 1;
}
}
}