tags:

views:

286

answers:

2

I have read the posts:

http://stackoverflow.com/questions/320510/viewing-contents-of-a-jar-file
and
http://stackoverflow.com/questions/1429172/list-files-inside-a-jar

But I, sadly, couldn't find a good solution to actually read a JAR's content (file by file).

Furthermore, could someone give me a hint, or point to a resource, where my problem is discussed?

I just could think of a not-so-straight-forward-way to do this:
I could somehow convert the list of a JAR's resources to a list of inner-JAR URLs, which I then could open using openConnection().

+4  A: 

You use JarFile to open a Jar file. With it you can get ZipEntry or JarEntry (they can be seen as the same thing) by using 'getEntry(String name)' or 'entires'. Once you get an Entry, you can use it to get InputStream by calling 'JarFile.getInputStream(ZipEntry ze)'. Well you can read data from the stream.

See a tutorial here.

NawaMan
Ah, thank you! I've somehow totally overseen JarFile's getInputStream!
ivan_ivanovich_ivanoff
+1  A: 

Here is how I read it as a ZIP file,

   try {
        ZipInputStream is = new ZipInputStream(new FileInptuStream("file.jar"));
        ZipEntry ze;

        byte[] buf = new byte[4096];
        int len;

        while ((ze = is.getNextEntry()) != null) {

            System.out.println("----------- " + ze);
            len = ze.getSize();

            // Dump len bytes to the file
            ...
        }
        is.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

This is more efficient than JarFile approach if you want decompress the whole file.

ZZ Coder