我正在使用FileChannel
和FileInputStream
做一个简单的文件复制从File
从文件到File
文件。NIO GZIP压缩和复制文件
的代码基本上是这样的:
source = new FileInputStream(fromFile).getChannel();
destination = new FileOutputStream(toFile, false).getChannel(); // overwrite
destination.transferFrom(source, 0, source.size());
其中fromFile
和toFile
是正确File
对象。 现在,我不想直接从fromFile
进行复制,而是想使用GZIP(在Java库中找到)压缩其内容,然后复制到toFile
。另外相反,当我从toFile
转回来时,我也想解压缩它。
我想知道是否有像
source = new GZIPCompressInputStream(new FileInputStream(fromFile)).getChannel();
或
source = new GZIPDecompressInputStream(new FileInputStream(fromFile)).getChannel();
和代码的所有其余保持不变的简单方法。你有没有建议最干净的解决方案?
谢谢..
你使用Java 7+吗? – fge 2015-03-31 10:11:53
是的,我用Java 7+ – bozeng 2015-03-31 10:12:25
[从FileChannel(Java NIO)读一个GZIP文件]中可能重复(http://stackoverflow.com/questions/3335969/reading-a-gzip-file-from-a-filechannel -java-nio) – 2015-03-31 20:42:53