Reputation: 319
I'm setting up a function where I collect a list of apk files, then inside a For-loop, I declare a ZipFile intense to check the ABI Directories under the lib folder, by checking if any file ends with ".so". The only option I can solution I can think of is to use regex String expressions
I've made several regex expression attempts with \b, [A-Za-z-0-9]*, but I keep getting NullPointerException. I researched solutions for using regex with File names, but not really cases for ZipFile and ZipEntry classes. At the moment, here is my current approach with Regex. (Sometimes the regex expressions make my head spin.)
public void createAPKList() throws ZipException, IOException {
apkFileList = new File("C:/eclipse/workspace/Appium-Side-Project/APKDir/APKFiles").listFiles();
System.out.println(apkFileList[apkFileList.length-1].getPath());
apkInstallOptions = storeAndSortFiles(apkFileList, apkInstallOptions);
}
private HashMap<String, File> storeAndSortFiles(File[] fileList, HashMap<String, File> storageList) throws ZipException, IOException{
ZipFile apkDetails;
//Example of calling specific ABI
String aBI = "armeabi-v7a/";
String patternStr = "^lib/"+aBI+"[(.*)].so$";
for(File apk : fileList) {
//how to initialize ZipEntry to assign the any file that ends with ".so" ?
apkDetails = new ZipFile(apk);
ZipEntry readFolders = apkDetails.getEntry(patternStr);
System.out.println(readFolders.getName().startsWith("lib/armeabi-v7a/"));
}
return storageList;
}
The current solution gives a NullPointerException since the ZipEntry value is null. I'm not sure if the error is from my regex expression or the expression is working, but for cases where more than one ".so" exists in the directory. Is it even possible to use Regex in ZipFile/ZipEntry?
Upvotes: 1
Views: 1078
Reputation: 44338
In a word: No.
Patterns are not supported, and in fact there is nothing in the documentation that implies a pattern is allowed. You must iterate through all entries in the ZipFile, and check whether each one matches:
Enumeration<? extends ZipEntry> entries = apkDetails.entries();
while (entries.hasMoreElements()) {
ZipEntry entry = entries.nextElement();
String name = entry.getName();
if (entry.startsWith("lib/armeabi-v7a/") && entry.endsWith(".so")) {
// Matching entry found.
}
}
There’s an old saying that when one decides to solve a problem with regular expressions, one goes from having one problem to having two problems. It’s best not to use regular expressions if you don’t need them. In your case, startsWith
and endsWith
are sufficient.
I’m not sure if you need to do something with the matching entries, or if you just want to check for their existence. If it’s the latter case, you can simplify the code using a Stream:
boolean hasMatchingEntries =
apiDetails.stream().map(ZipEntry::getName).anyMatch(
name -> name.startsWith("lib/armeabi-v7a/") && name.endsWith(".so"));
Upvotes: 3