我使用FileChannel
和FileInputStream
进行从File
从文件到File
到文件的简单文件复制。
代码基本上是这样的:
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();
并且所有其余代码保持不变。你对这个最干净的解决方案有什么建议吗?
谢谢..
答案 0 :(得分:0)
您可以将FileInputStream
包裹在GZIPInputStream
中,如此:
InputStream input = new GZIPInputStream(yourCompressedInput);
对于写入时的压缩,您应该使用GZIPOutputStream。
祝你好运。