list Jar File Entries - Java Reflection

Java examples for Reflection:Jar

Description

list Jar File Entries

Demo Code

/*//from   w  w w.j  a  va2  s. c  om
 * Copyright (c) 2011 - 2012. Elega9t Ltd. All rights reserved.
 * ELEGA9T PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.Copyright (c) 2011 - 2012. Elega9t Ltd. All rights reserved.
 */
//package com.java2s;
import java.io.*;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;

public class Main {
    public static List<String> listEntries(File file, FilenameFilter filter)
            throws IOException {
        try {
            JarFile jarFile = new JarFile(file);
            List<String> list = new ArrayList<String>();
            Enumeration<JarEntry> entries = jarFile.entries();
            while (entries.hasMoreElements()) {
                JarEntry entry = entries.nextElement();
                if (filter.accept(file, entry.getName())) {
                    list.add(entry.getName());
                }
            }
            return list;
        } catch (IOException e) {
            throw new IOException(file.getAbsolutePath(), e);
        }
    }
}

Related Tutorials