开发者

Reading a GZIP file from a FileChannel (Java NIO)

开发者 https://www.devze.com 2023-01-08 17:21 出处:网络
I need to read/unpack a .gz file given a FileChannel. I\'ve p开发者_如何转开发layed around with extracting GZIP archives using GZIPInputStream, but this won\'t take a FileChannel. I don\'t have acce

I need to read/unpack a .gz file given a FileChannel.

I've p开发者_如何转开发layed around with extracting GZIP archives using GZIPInputStream, but this won't take a FileChannel. I don't have access to the original FileInputStream that the FileChannel was taken from.

If someone could tell me a good way (or at least any way) of reading GZIP from a FileChannel, I would greatly appreciate it.

Adapted from a question on the Sun Oracle forums.


You could obtain a wrapping InputStream around the FileChannel:

FileChannel fc = ...
GZIPInputStream gis = new GZIPInputStream(Channels.newInputStream(fc));

Channels is in Java SE.


Yes, there is a solution. Implementing GZip is fairly simple. You need a Inflater and CRC32, plus reading the header/trailer. Unfortunately java.util.zip.Inflater takes only byte[] which is suboptimal (a direct Buffer would have been times more efficient). Yet, using non-direct buffer and ByteBuffer.array() is an option.

The built in java.util.zip.CRC32 is quite slow and reimplementing it in java is a nice step towards performance. com.jcraft.jzlib offers pure java implementation (almost looks like pure C, though) and it's possible to replace the byte[] w/ direct Buffer. The library is somewhat slower compared to inflater (decomppress) due to too many bounds check and inability to perform so deep inline. Yet, it's faster on compression.

0

精彩评论

暂无评论...
验证码 换一张
取 消