简体   繁体   中英

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.

What my problem here is I can read the contents jar file but I am unable to check the entries as per my requirement.

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.

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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