The easiest way to unzip a jar in java

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

What is the easiest way to do this? I wish to use a free third-party library if necessary.

+3
source share
5 answers

You can use java.util.jar.JarFile to iterate over the records in a file, extracting them through an InputStream and writing data to an external file. Apache Commons IO provides utilities to make this a little less awkward.

+6
source
 ZipInputStream in = null; OutputStream out = null; try { // Open the jar file String inFilename = "infile.jar"; in = new ZipInputStream(new FileInputStream(inFilename)); // Get the first entry ZipEntry entry = in.getNextEntry(); // Open the output file String outFilename = "o"; 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); } } catch (IOException e) { // Manage exception } finally { // Close the streams if (out != null) { out.close(); } if (in != null) { in.close(); } } 
+4
source

Jar is mostly encrypted using the ZIP algorithm, so you can use winzip or winrar to extract.

If you are looking for a programmatic way then the correct answer will be more correct.

+2
source

From a command line like jar xf foo.jar or unzip foo.jar

+1
source

Use Ant unzip task .

+1
source

Source: https://habr.com/ru/post/1390088/


All Articles