Easiest way to unpack a jar in java

前端 未结 6 1089
遇见更好的自我
遇见更好的自我 2021-01-20 09:30

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

6条回答
  •  滥情空心
    2021-01-20 09:51

    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();
        }
    }
    

提交回复
热议问题