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.
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.
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.
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.
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) {
}