【问题标题】:Easiest way to unpack a jar in java在java中解压jar的最简单方法
【发布时间】:2019-03-04 02:26:27
【问题描述】:

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

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

【问题讨论】:

    标签: java jar


    【解决方案1】:

    您可以使用java.util.jar.JarFile 遍历文件中的条目,通过其InputStream 提取每个条目并将数据写入外部文件。 Apache Commons IO 提供了一些实用程序来让这变得不那么笨拙。

    【讨论】:

      【解决方案2】:
      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】:

        jar基本上是用ZIP算法压缩的,所以你可以用winzip或者winrar来解压。

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

        【讨论】:

          【解决方案4】:

          从命令行输入jar xf foo.jarunzip foo.jar

          【讨论】:

            【解决方案5】:

            使用 Ant unzip task

            【讨论】:

              【解决方案6】:

              这是我在 Scala 中的版本,与 Java 中的版本相同,解压到单独的文件和目录中:

              import java.io.{BufferedInputStream, BufferedOutputStream, ByteArrayInputStream}
              import java.io.{File, FileInputStream, FileOutputStream}
              import java.util.jar._
              
              def unpackJar(jar: File, target: File): Seq[File] = {
                val b   = Seq.newBuilder[File]
                val in  = new JarInputStream(new BufferedInputStream(new FileInputStream(jar)))
              
                try while ({
                  val entry: JarEntry = in.getNextJarEntry
                  (entry != null) && {
                    val f = new File(target, entry.getName)
                    if (entry.isDirectory) {
                      f.mkdirs()
                    } else {
                      val bs  = new BufferedOutputStream(new FileOutputStream(f))
                      try {
                        val arr = new Array[Byte](1024)
                        while ({
                          val sz = in.read(arr, 0, 1024)
                          (sz > 0) && { bs.write(arr, 0, sz); true }
                        }) ()
                      } finally {
                        bs.close()
                      }
                    }
                    b += f
                    true
                  }
                }) () finally {
                  in.close()
                }
              
                b.result()
              }
              

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2014-08-28
                • 2013-12-19
                • 1970-01-01
                • 1970-01-01
                • 2023-03-17
                • 1970-01-01
                相关资源
                最近更新 更多