Solved

list directory in jar files

Posted on 2001-07-17
1
179 Views
Last Modified: 2012-06-27
String[] fileNames = dir.list();

is used for normal folders.  How to put list of files in array for JAR files??
0
Comment
Question by:TLTEO
1 Comment
 
LVL 19

Accepted Solution

by:
Jim Cakalic earned 15 total points
ID: 6291231
Jar files are simply Zip files with a manifest. Here is one example of how to do this.

import java.util.zip.*;
import java.util.*;
import java.io.*;

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();
            list.add(entry.getName());
        }
        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) {
            System.out.println(names[i]);
        }

    }
}

Best regards,
Jim Cakalic
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Opening PDF on button click and fill new document 2 47
hibernate jars 4 45
iterator/ListIterator approach 17 39
by zero exception 10 52
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question