Search code examples
javazipfile-existsnio2

How to check that file exists inside a zip archive?


How to check that file exists inside a zip archive?
For example, check whether app.apk contains classes.dex.
I want to find a solution that uses Java NIO.2 Path and without extracting the whole archive if possible.

I've tried and it didn't work:

Path classesFile = Paths.get("app.apk", "classes.dex");  // apk file with classes.dex
if (Files.exists(apkFile))  // false!
    ...

Solution

  • My solution is:

    Path apkFile = Paths.get("app.apk");
    FileSystem fs = FileSystems.newFileSystem(apkFile, null);
    Path dexFile = fs.getPath("classes.dex");
    if (Files.exists(dexFile))
        ...