Tuesday, August 7, 2012

List all xml files from the JAR.

Do you want to know which all files are in the jar without extracting it.?

This is a small tutorial where I am listing all the xml files.

import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

/**
 * List the name of all xml which is in given jars
 *
 * @author abdul
 */
public class ListXmlFiles {
    /* Stores the xml file name*/
    List<String> list = new ArrayList<String>();

    public List<String> getList() {
        return list;
    }

    /**
     * @param args
     * @throws IOException
     */
    public static void main(String[] args) throws IOException {
        ListXmlFiles xmlFiles = new ListXmlFiles();
        JarFile jarFile = new JarFile("./lib/dummyJar.jar");
        Enumeration<JarEntry> e = jarFile.entries();
        while (e.hasMoreElements()) {
            xmlFiles.process(e.nextElement());
        }
        System.out.println("List:" + xmlFiles.getList());
    }

    private void process(Object obj) {
        JarEntry entry = (JarEntry)obj;
        String name = entry.getName();
        if(name.endsWith(".xml")) {
            list.add(name);      
        }
    }
}

If you want to know all the files name inside,Just removed the if-else filter, It will add all  the filename into the list.
private void process(Object obj) {
        JarEntry entry = (JarEntry)obj;
        String name = entry.getName();
        list.add(name);        

}


No comments:

Post a Comment

How TOPT Works: Generating OTPs Without Internet Connection

Introduction Have you ever wondered how authentication apps like RSA Authenticator generate One-Time Passwords (OTPs) without requiring an i...