tags:

views:

306

answers:

5

Basically, I have a jar file that i want to unzip to a specific folder from a junit test.

What is the easiest way to do this? I am willing to use a free third party library if it's necessary.

+3  A: 

You could use java.util.jar.JarFile to iterate over the entries in the file, extracting each one via its InputStream and writing the data out to an external File. Apache Commons IO provides utilities to make this a bit less clumsy.

skaffman
+1  A: 

Jar is basically zipped using ZIP algorithm, so you can use winzip or winrar to extract.

If you are looking for programmatic way then the first answer is more correct.

jatanp
Will not work in OP's case of executing from a junit test.
Chadwick
A: 

From command line type jar xf foo.jar or unzip foo.jar

Clinton Bosch
+2  A: 
try {
    // Open the jar file
    String inFilename = "infile.jar";
    ZipInputStream in = new ZipInputStream(new FileInputStream(inFilename));

    // Get the first entry
    ZipEntry entry = in.getNextEntry();

    // Open the output file
    String outFilename = "o";
    OutputStream out = new FileOutputStream(outFilename);

    // Transfer bytes from the ZIP file to the output file
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }

    // Close the streams
    out.close();
    in.close();
} catch (IOException e) {
}
KingInk
+1  A: 

Use the unzip task in ant.

http://ant.apache.org/manual/CoreTasks/unzip.html

Thorbjørn Ravn Andersen