如何在Zipfile目录中查找任何文件 [英] How to find any file within a Directory of a Zipfile

查看:189
本文介绍了如何在Zipfile目录中查找任何文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在设置一个函数,在其中收集apk文件列表,然后在For循环中,我声明一个ZipFile密集型文件,以检查lib文件夹下的ABI目录,方法是检查文件是否以."结尾.所以".我能想到的唯一解决方案是使用regex字符串表达式

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

我已经用\ b,[A-Za-z-0-9] *进行了几次正则表达式尝试,但是我一直收到NullPointerException.我研究了使用带有文件名的正则表达式的解决方案,但对于ZipFile和ZipEntry类的情况却并非如此.目前,这是我目前使用Regex的方法. (有时,正则表达式使我头昏脑胀.)

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;
}

由于ZipEntry值为null,因此当前解决方案给出NullPointerException.我不确定错误是由于我的regex表达式还是表达式起作用,但是对于目录中存在多个".so"的情况.甚至可以在ZipFile/ZipEntry中使用Regex吗?

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?

推荐答案

总之:否.

不支持模式,实际上

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.
    }
}

有一句老话,当人们决定用正​​则表达式解决问题时,一个问题就从一个问题变成了两个问题.如果您不需要正则表达式,则最好不要使用它们.就您而言,startsWithendsWith就足够了.

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"));

这篇关于如何在Zipfile目录中查找任何文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆