tags:

views:

414

answers:

4

I have a jar or war.

I'm programmaticaly reading this jar, and when I find jar inside this jar I'd like to programmaticaly read it again.

But JarFile provides only getInputStream, which I cannot pass to JarFile(File file) constructor.

How to read jar from jar?

EDIT: I was thinking about getting the File somehow from classloader or so.

+1  A: 

you can create jar file in File System, something like

 File tempFile=TempFile.createFile("newJar",".jar");

and write Stream into it. After that you can construct your JarFile(tempFile) and handle it...

Forget about it if program is running as unsigned applet/JNLP since you will not have right to create file in file system...

ante.sabo
I just wonder, is it possible to do that just in memory?
blefesd
yes this works, but I'm not sure if it's the best to write temp file somewhere. and if there is not any other solution without creating temp file
blefesd
don't know; file has to be in file system (don't beleive there is something like InMemoryFile :)) and they've made it that way on purpose you cannot instance JarFile without passing file inside..
ante.sabo
A: 

Update: Sorry this is probably too late for your needs, I just spotted your last question in the comments though. So I've modified the example to show each nested entry being copied directly to an OutputStream without any need to inflate the outer jar.

In this case the OutputStream is System.out but it could be any OutputStream (e.g. to a file...).


There's no need to use a temporary file. You can use JarInputStream instead of JarFile, pass the InputStream from the outer entry to the constructor and you can then read the contents of the jar.

For example:

JarFile jarFile = new JarFile(warFile);

Enumeration entries = jarFile.entries();

while (entries.hasMoreElements()) {
    JarEntry jarEntry = (JarEntry) entries.nextElement();

    if (jarEntry.getName().endsWith(".jar")) {
        JarInputStream jarIS = new JarInputStream(jarFile
                .getInputStream(jarEntry));

        // iterate the entries, copying the contents of each nested file 
        // to the OutputStream
        JarEntry innerEntry = jarIS.getNextJarEntry();

        OutputStream out = System.out;

        while (innerEntry != null) {
            copyStream(jarIS, out, innerEntry);
            innerEntry = jarIS.getNextJarEntry();
        }
    }
}

...

/**
 * Read all the bytes for the current entry from the input to the output.
 */
private void copyStream(InputStream in, OutputStream out, JarEntry entry)
        throws IOException {
    byte[] buffer = new byte[1024 * 4];
    long count = 0;
    int n = 0;
    long size = entry.getSize();
    while (-1 != (n = in.read(buffer)) && count < size) {
        out.write(buffer, 0, n);
        count += n;
    }
}
Rich Seller
I think you cannot process JarEntry as ZipFile and get entries() and work with it.
blefesd
Try it, it works. A jar is just a zip with a different extension and a convention that it has a manifest.
Rich Seller
but from JarEntry or ZipEntry you cannot obtain all entries in included jar.Like this: JarFile jf = new JarFile(jarName); Enumeration<JarEntry> en = jf.entries(); I needed to recursively walk the jars and search in those jars. From JarEntry I cannot get entries(); Or at least I haven't found how.
blefesd
that's what the last part of the second snippet does isn't it? or have I missed something?
Rich Seller
if you're looking for a means to identify the entries of a nested jar without iterating/recursing, I don't think there is any such method. The last line of the snippet above gives you an Enumeration of the inner entries, this is equivalent to calling entries() on a JarFile
Rich Seller
Hi, I might be missing something, I'm really able to get the inner entries, but I don't know how to write this inner entry(file) to the e.g. stdout.
blefesd
A: 

Hi,

Recursively use the JarFile again to read the new jar file. For ex.,

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.jar.JarEntry;
import java.util.jar.JarFile;
import java.util.zip.ZipEntry;


public class JarReader {

public static void readJar(String jarName) throws IOException {
    String dir = new File(jarName).getParent();
    JarFile jf = new JarFile(jarName);
    Enumeration<JarEntry> en = jf.entries();
    while(en.hasMoreElements()) { 
        ZipEntry ze = (ZipEntry)en.nextElement();
        if(ze.getName().endsWith(".jar")) { 
            readJar(dir + System.getProperty("file.separator") + ze.getName());
        } else {
            InputStream is = jf.getInputStream(ze);
            // ... read from input stream
            is.close();
            System.out.println("Processed class: " + ze.getName());
        }   
    }
}

public static void main(String[] args) throws IOException {
    readJar(args[0]);
}

}
prasadvk
This doesn't work JarFile jf = new JarFile(jarName); JarFile expect the jarName(file) on the system. And you will get java.util.zip.ZipException: The system cannot find the path specified
blefesd
A: 

Generally it is a matter of getting to an InputStream and then work with that. This allows you to abstract from most "jar in jar on web server" issues and similar.

Apparently in this case it is JarFile.getInputStream() and JarInputStream().

Thorbjørn Ravn Andersen