从文件通道创建 Zip 存档
Create Zip archive from Filechannels
我正在尝试将文件添加到 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 文件?
您可以使用 Channels.newChannel
方法将任何 OutputStream 转换为 Channel。
事实证明,这非常容易做到。不要使用频道或任何东西。只需创建一个 zip 文件系统并将文件复制过来。
http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html
我正在尝试将文件添加到 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 文件?
您可以使用 Channels.newChannel
方法将任何 OutputStream 转换为 Channel。
事实证明,这非常容易做到。不要使用频道或任何东西。只需创建一个 zip 文件系统并将文件复制过来。
http://docs.oracle.com/javase/7/docs/technotes/guides/io/fsp/zipfilesystemprovider.html