Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/37.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将JarOutputStream添加到ZipOutputStream_Java_Outputstream_Zipoutputstream - Fatal编程技术网

Java 将JarOutputStream添加到ZipOutputStream

Java 将JarOutputStream添加到ZipOutputStream,java,outputstream,zipoutputstream,Java,Outputstream,Zipoutputstream,目前,我已经生成了一个JarOutputStream和一个填充了*.java文件的目录 我想将最终的JarOutputStream添加到ZipOutputStream中,以返回最终的*.zip文件,其中包含目录和*.jar文件 现在我想知道,如何以及是否可以将JarOutputStream添加到ZipOutputStream 非常感谢 我不知道你所说的“我已经生成了一个JarOutputStream”到底是什么意思。但是,如果您想将内容写入JAR文件,然后再将内容写入ZIP文件,而不需要将所有内

目前,我已经生成了一个JarOutputStream和一个填充了*.java文件的目录

我想将最终的JarOutputStream添加到ZipOutputStream中,以返回最终的*.zip文件,其中包含目录和*.jar文件

现在我想知道,如何以及是否可以将JarOutputStream添加到ZipOutputStream


非常感谢

我不知道你所说的“我已经生成了一个JarOutputStream”到底是什么意思。但是,如果您想将内容写入JAR文件,然后再将内容写入ZIP文件,而不需要将所有内容都保存在内存中,则可以通过以下方式执行此操作:

public static class ExtZipOutputStream extends ZipOutputStream {

  public ExtZipOutputStream(OutputStream out) {
    super(out);
  }

  public JarOutputStream putJarFile(String name) throws IOException {
    ZipEntry zipEntry = new ZipEntry(name);
    putNextEntry(zipEntry);
    return new JarOutputStream(this) {

      @Override
      public void close() throws IOException {
        /* IMPORTANT: We finish writing the contents of the ZIP output stream but do 
         * NOT close the underlying ExtZipOutputStream
         */
        super.finish();
        ExtZipOutputStream.this.closeEntry();
      }
    };
  }
}

public static void main(String[] args) throws FileNotFoundException, IOException {

  try (ExtZipOutputStream zos = new ExtZipOutputStream(new FileOutputStream("target.zip"))) {
    
    try (JarOutputStream jout = zos.putJarFile("embed.jar")) {
      /*
       * Add files to embedded JAR file here ...
       */
    }
    
    /*
     * Add additional files to ZIP file here ...
     */
    
  }
}

我不确定你所说的“我已经生成了一个JarOutputStream”到底是什么意思。但是,如果您想将内容写入JAR文件,然后再将内容写入ZIP文件,而不需要将所有内容都保存在内存中,则可以通过以下方式执行此操作:

public static class ExtZipOutputStream extends ZipOutputStream {

  public ExtZipOutputStream(OutputStream out) {
    super(out);
  }

  public JarOutputStream putJarFile(String name) throws IOException {
    ZipEntry zipEntry = new ZipEntry(name);
    putNextEntry(zipEntry);
    return new JarOutputStream(this) {

      @Override
      public void close() throws IOException {
        /* IMPORTANT: We finish writing the contents of the ZIP output stream but do 
         * NOT close the underlying ExtZipOutputStream
         */
        super.finish();
        ExtZipOutputStream.this.closeEntry();
      }
    };
  }
}

public static void main(String[] args) throws FileNotFoundException, IOException {

  try (ExtZipOutputStream zos = new ExtZipOutputStream(new FileOutputStream("target.zip"))) {
    
    try (JarOutputStream jout = zos.putJarFile("embed.jar")) {
      /*
       * Add files to embedded JAR file here ...
       */
    }
    
    /*
     * Add additional files to ZIP file here ...
     */
    
  }
}

你不能这样做-它不能正常工作。原因是ZIP和JAR(JAR文件本质上是另一个扩展名的ZIP文件)正在压缩您放入其中的文件中的原始数据。因此,您可以将ZIP文件的内容发送到JarOutputStream,反之亦然。但是您必须先创建文件(尽管您可以在内存缓冲区中创建文件),否则无法执行此操作-它无法正常工作。原因是ZIP和JAR(JAR文件本质上是另一个扩展名的ZIP文件)正在压缩您放入其中的文件中的原始数据。因此,您可以将ZIP文件的内容发送到JarOutputStream,反之亦然。但是您必须先创建文件(尽管您可以在内存缓冲区中创建文件)