InvisibleExo
InvisibleExo

Reputation: 319

ZipEntry returns null When Calling for a Directory Inside an APK File

I'm trying to setup a function where apk files are placed in a specified directory, stored into an array[], then go through a for-each loop, creating a ZipFile instance then create a ZipEntry to call for the lib directory, and read the file names inside to help determine which apks are compatible for androids devices currently connected to PC. This is the code I have so far:

public File[] apkFileList;
private HashMap<String, File> apkInstallOptions = new HashMap<String, File>();

public void createAPKList() throws ZipException, IOException {
    apkFileList =  new File(".....(many dirs later)/APKDir/APKFiles").listFiles();
    System.out.println(apkFileList[apkFileList.length-1].getName());
    apkInstallOptions = storeAndSortFiles(apkFileList, apkInstallOptions);
}

private HashMap<String, File> storeAndSortFiles(File[] fileList, HashMap<String, File> storageList) throws ZipException, IOException{
    ZipFile apkDetails;
    for(File apk : fileList) {
        apkDetails = new ZipFile(apk);
        ZipEntry libFiles = apkDetails.getEntry("lib/");
        libFiles.isDirectory();
        InputStream inputStream = apkDetails.getInputStream(libFiles);
        BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
        String line = null;
        while((line = br.readLine()) != null) {
            System.out.println(line);

        }

    }
    return storageList;
}

When I run the code to test out if the mapping to the path looks good, I get the correct name for apk, but when I attempt to call for ZipEntry for the lib directory, it returns Null. I'm not sure what I'm forgetting and what I'm not considering to add in order to read the contents inside. I've seen solutions where the Zip/apk file was extracted and then read, but I only want to read the contents inside the lib directory. Is that possible?

Screenshot of what is seen inside the APK file that a lib file does exist (used 7-Zip to look inside): enter image description here

Upvotes: 0

Views: 309

Answers (1)

Pierre
Pierre

Reputation: 17417

Directories do not exist in the zip format. They're artificially shown in desktop tools, but zip libraries only show the actual files that are present, and that excludes directories.

The files present in an APK would thus be for example:

lib/arm64-v8a/libhello.so
lib/armeabi-v7a/libhello.so

But there would be no entries for lib or lib/armeabi-v7a.

You can thus test if a ZipEntry is under the "lib" directory by calling:

zipEntry.getName().startsWith("lib/");

Upvotes: 1

Related Questions