代码之家  ›  专栏  ›  技术社区  ›  pbreault

在Java中打开jar的最简单方法

  •  3
  • pbreault  · 技术社区  · 15 年前

    基本上,我有一个JAR文件,我想从JUnit测试中解压缩到一个特定的文件夹。

    最简单的方法是什么? 如有必要,我愿意使用免费的第三方图书馆。

    5 回复  |  直到 13 年前
        1
  •  6
  •   skaffman    15 年前

    你可以使用 java.util.jar.JarFile 迭代文件中的条目,通过 InputStream 将数据写入外部文件。ApacheCommonsIO提供了一些实用程序,使其不那么笨拙。

        2
  •  4
  •   Community CDub    13 年前
    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();
        }
    }
    
        3
  •  2
  •   jatanp    15 年前

    jar基本上是使用zip算法进行压缩的,所以您可以使用winzip或winrar进行提取。

    如果您正在寻找编程方法,那么第一个答案更正确。

        4
  •  1
  •   Clinton Bosch    15 年前

    从命令行类型 jar xf foo.jar unzip foo.jar

        5
  •  1
  •   martin clayton egrunin    14 年前

    使用蚂蚁 unzip task .