我正在使用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