Using Commons Compress would be simpler, not least because it has reasonable common interfaces between various decompressors that make life easier + allows you to simultaneously process other compression formats (e.g. Tar)
If you only want to use the native Zip support, I would suggest you do something like this:
File file = new File("outermost.zip"); FileInputStream input = new FileInputStream(file); check(input, file.toString()); public static void check(InputStream compressedInput, String name) { ZipInputStream input = new ZipInputStream(compressedInput); ZipEntry entry = null; while ( (entry = input.getNextEntry()) != null ) { System.out.println("Found " + entry.getName() + " in " + name); if (entry.getName().endsWith(".zip")) {
Your code will fail because you are trying to read inner.zip inside outer.zip as a local file, but it does not exist as a separate file. The above code will handle everything ending in .zip like another zip file and will be recursive
You probably want to use combo compression, so you can handle things with alternative file names, other compression formats, etc.
source share