简体   繁体   English

检查jar文件的内容

[英]check the contents of a jar file

I have a situation where i need to read the contents of Jar file and check the contents in it. 我有一种情况,我需要读取Jar文件的内容并检查其中的内容。

What my problem here is I can read the contents jar file but I am unable to check the entries as per my requirement. 我在这里的问题是我可以读取内容jar文件,但无法根据我的要求检查条目。

My requirement is I should allow the jars which hava an extension of .xml , .png or .jpg or .jpeg and .MF file Here is my code. 我的要求是,我应该允许罐子具有.xml.png or .jpg or .jpeg.MF文件的扩展名。这是我的代码。

JarFile connectorjarfile = new JarFile(jarFileStorageocation);
Enumeration<JarEntry> jarEntries1 = connectorjarfile.entries();
int count = 0;
while (jarEntries1.hasMoreElements()) {
    System.out.println("Before Jar Validation is going on");
    JarEntry jarEntry = (JarEntry) jarEntries1.nextElement();
    System.out.println(jarEntry.getName());
    String msg1 = "valid Jar File";
    String msg2 = "Invalid Jar File";

    if ((jarEntry.getName().endsWith(".png") || jarEntry.getName().endsWith(".jpg") || jarEntry.getName().endsWith(".jpeg")) || jarEntry.getName().endsWith(".xml") || jarEntry.getName().endsWith(".MF") || jarEntry.getName().equals("META-INF/")) {
        try {
            response.getWriter().write(msg1);
            break;
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        count++;

        try {
            response.getWriter().write(msg2);
            break;
        } catch (IOException e) {
            e.printStackTrace();
        }
        //break;
    }

The If loop is executing so many times because it checks the contents of Jar file one by one. If循环执行了很多次,因为它一个接一个地检查Jar文件的内容。 I want to check them one at a time and then enter the loop. 我想一次检查一次,然后进入循环。 suggest me. 建议我。

I want to know only whether it is valid or invalid 我只想知道它是否有效

Then, you only need to iterate until you find an invalid file. 然后,您只需要迭代直到找到无效的文件。 The main thing you need to change with your code is to move the actions from your if / else blocks outside the loop - inside the loop, simply set a flag to indicate when you found an invalid entry: 您需要用代码更改的主要内容是将操作从if / else块移到循环 -在循环内,只需设置一个标志以指示何时发现无效条目:

boolean isValid = true;
while (isValid && jarEntries1.hasMoreElements()) {
    JarEntry jarEntry = jarEntries1.nextElement();

    isValid = jarEntry.getName().endsWith(".png")  || jarEntry.getName().endsWith(".jpg") || 
              jarEntry.getName().endsWith(".jpeg") || jarEntry.getName().endsWith(".xml") || 
              jarEntry.getName().endsWith(".MF")   || jarEntry.getName().equals("META-INF/");
}

if (isValid) {
    // do whatever you want if the file is valid
} else {
    // do whatever you want if the file is not valid
}

Side note: 边注:

You do not need to explicitly cast the result from nextElement() to JarEntry - jarEntries1 is already an Enumeration of type JarEntry . 您无需将nextElement()的结果显式转换为JarEntry - jarEntries1已经是类型JarEntryEnumeration

Why do you need to count all the files that are not valid? 为什么需要计算所有无效的文件? You can't stop the loop with something like : 您无法使用以下命令停止循环:

String name = jarEntry.getName()
if ! (name.endsWith(".png")....) {
   // invalid , let's stop
   break;
} 

In the worst case, you must iterate only one time. 在最坏的情况下,您只能迭代一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM