Solved

list directory in jar files

Posted on 2001-07-17
1
177 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now