Java 从FileChannel创建Zip存档

Java 从FileChannel创建Zip存档,java,nio,Java,Nio,我正在尝试将文件添加到zip存档。我想做这样的事情 public void zipFile(Path fileToZip, Path zipFile) { ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile, CREATE, APPEND)); FileChannel outputChannel = new FileOutputStream(zipOut).getChannel()

我正在尝试将文件添加到zip存档。我想做这样的事情

public void zipFile(Path fileToZip, Path zipFile) {
    ZipOutputStream zipOut = new ZipOutputStream(Files.newOutputStream(zipFile, CREATE, APPEND));
    FileChannel outputChannel = new FileOutputStream(zipOut).getChannel() //How to go from zipoutputstream to FileChannel...
    FileChannel inputChannel = FileChannel.open(zipFile, READ)
    ZipEntry zipEntry = new ZipEntry(fileToZip.toString());
    zipOut.putNextEntry(zipEntry);

    inputChannel.transferTo (0, inputChannel.size(), outputChannel);

    outputChannel.close();
    inputChannel.close();
}

但是ZipOutputStream不像FileOutputStream那样具有getChannel()。如何使用通道创建zip文件?

您可以使用该方法将任何OutputStream转换为通道。

您可以使用该方法将任何OutputStream转换为通道。

事实证明,这非常容易做到。不要使用频道或任何东西。只需创建一个zipfilesystem并复制文件即可


事实证明,这很容易做到。不要使用频道或任何东西。只需创建一个zipfilesystem并复制文件即可

可能重复的可能重复的