list directory in jar files

String[] fileNames = dir.list();

is used for normal folders.  How to put list of files in array for JAR files??
Who is Participating?
Jim CakalicConnect With a Mentor Senior Developer/ArchitectCommented:
Jar files are simply Zip files with a manifest. Here is one example of how to do this.

import java.util.*;

public class ZipList {
    ZipFile _zipfile;

    public ZipList(File file) throws ZipException, IOException {
        _zipfile = new ZipFile(file);

    public String[] list() {
        ArrayList list = new ArrayList(_zipfile.size());
        Enumeration enum = _zipfile.entries();
        while (enum.hasMoreElements()) {
            ZipEntry entry = (ZipEntry)enum.nextElement();
        return (String[])list.toArray(new String[list.size()]);
    public static void main(String[] args) throws Exception {
        ZipList zip = new ZipList(new File(args[0]));
        String[] names = zip.list();
        for (int i = 0; i < names.length; ++i) {


Best regards,
Jim Cakalic
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.