I'm working on a java project that requires me to access a file within multiple embedded zip files and directories.
For example, archive1.zip/archive1/archive2.zip/archive2/directory1/file_that_I_need.txt.
It would be a lot easier if when each zip file was extracted, it would immediately list its contents but instead there's a folder inside that contains all the contents.
The examples I found online deal with zip files that, when extracted, contain the files they need to access but I can't find any that deal with accessing files within a directory in a zip file. Any advice on this would be great.
Thanks!
Given the prohibition against creating new files, you're pretty much stuck with ZipInputStream. When you find the ZipEntry
that corresponds to the embedded archive, you then read its stream to find the actual file. You can proceed recursively through as many levels of archives as you want.
This works OK if you're looking to process a single file. However, re-reading the archives for multiple files can be expensive. A better solution is to at least open the outer archive as a ZipFile, which memory-maps the actual file.
If you can then extract the contained archives into a temporary directory and open them as ZipFile
s as well, you'll probably see a big speed increase (as long as you're pulling multiple files from each embedded archive).