Tempo fa ho scritto questo, che ha sempre funzionato. Anche questo usa isDirectory() per controllare che l'entry sia una directory e comportarsi di conseguenza... ha in più che puoi scegliere di decomprimere anche solo un "ramo" dell'albero di directory dello ZIP (indicato dalla stringa beginEntry):
codice:
private static final int BUFSIZE = 2048;
public static boolean decomprimi(File src, File dirOut, String beginEntry) {
boolean success = false;
ZipInputStream zipFile = null;
BufferedOutputStream bos = null;
try {
boolean prosegui = true;
if ( !dirOut.exists() ) {
if ( !dirOut.mkdirs() ) {
System.err.println("Impossibile creare la directory di destinazione.");
prosegui = false;
}
}
if ( prosegui ) {
zipFile = new ZipInputStream( new FileInputStream(src) );
ZipEntry entry = null;
while((entry = zipFile.getNextEntry()) != null) {
String entryName = entry.getName();
if ( entryName.startsWith(beginEntry) ) {
if ( entry.isDirectory() ) {
(new File(dirOut, entryName)).mkdirs();
} else {
int numBytes = 0;
byte[] data = new byte[BUFSIZE];
File entryOut = new File(dirOut, entryName);
FileOutputStream fos = new FileOutputStream( entryOut );
bos = new BufferedOutputStream(fos, BUFSIZE);
while((numBytes = zipFile.read(data, 0, BUFSIZE)) != -1) {
bos.write(data, 0, numBytes);
}
bos.flush();
bos.close();
}
}
}
success = true;
}
} catch (IOException ioe) {
ioe.printStackTrace();
success = false;
} finally {
if (zipFile != null) {
try {
zipFile.close();
} catch (IOException ioe) { ioe.printStackTrace(); }
}
}
return success;
}
Ciao.