Java utility library for processing embedded ZIP files

I am aware that Oracle notes that ZIP / GZIP files / compressors / decompressors are stored on its website on its website . But I have a scenario where I need to scan and find out if any nested ZIP / RARs are involved. For example, the following case:

-MyFiles.zip -MyNestedFiles.zip -MyMoreNestedFiles.zip -MoreProbably.zip -Other_non_zips -Other_non_zips -Other_non_zips 

I know that the compression package apache commons and java.util.zip are widely used packages in which the compiled comm actually serves the missing functions in java.util.zip, for example. some character settings when doing zipouts. But I'm not sure that utilities for recursing via zip file attachments and the answers provided in SO are not very good examples of this. I tried the following code (which I got from the Oracle blog), but, as I suspected, the subdirectory recursion failed because it simply could not find the files:

 public static void processZipFiles(String pathName) throws Exception{ ZipInputStream zis = null; InputStream is = null; try { ZipFile zipFile = new ZipFile(new File(pathName)); String nestPathPrefix = zipFile.getName().substring(0, zipFile.getName().length() -4); for(Enumeration e = zipFile.entries(); e.hasMoreElements();){ ZipEntry ze = (ZipEntry)e.nextElement(); if(ze.getName().contains(".zip")){ is = zipFile.getInputStream(ze); zis = new ZipInputStream(is); ZipEntry zentry = zis.getNextEntry(); while (zentry!=null){ System.out.println(zentry.getName()); zentry = zis.getNextEntry(); ZipFile nestFile = new ZipFile(nestPathPrefix+"\\"+zentry.getName()); if (zentry.getName().contains(".zip")) { processZipFiles(nestPathPrefix+"\\"+zentry.getName()); } } is.close(); } } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally{ if(is != null) is.close(); if(zis!=null) zis.close(); } } 

Maybe I'm doing something wrong - or using the wrong utilities. My goal is to determine if files or attached zip files received file extensions that I do not allow. This is done so that I can prevent my users from downloading prohibited files, even when they zip them. I also have the option of using Tika, which can do recursive parsing (using the Zukka Zitting solution), but I'm not sure if I can use the metadata for this discovery as I want.

Any help / suggestion is appreciated.

+6
source share
1 answer

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")) { // TODO Better checking check(input, name + "/" + entry.getName()); } } } 

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.

+2
source

All Articles