从 FileChannel 读取 GZIP 文件 (Java NIO)
我需要读取/解压给定 FileChannel 的 .gz 文件。
我尝试过使用 GZIPInputStream 提取 GZIP 档案,但这不需要 FileChannel。我无权访问从中获取 FileChannel 的原始 FileInputStream。
如果有人能告诉我一种从 FileChannel 读取 GZIP 的好方法(或至少任何方式),我将不胜感激。
I need to read/unpack a .gz file given a FileChannel.
I've played 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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以获得围绕 FileChannel 的包装 InputStream:
Channels 位于 Java SE 中。
You could obtain a wrapping InputStream around the FileChannel:
Channels is in Java SE.
是的,有一个解决方案。实现 GZip 相当简单。您需要充气机和 CRC32,以及阅读标题/预告片。不幸的是,
java.util.zip.Inflater
只需要byte[]
,这是次优的(直接Buffer的效率会高很多倍)。然而,使用非直接缓冲区和 ByteBuffer.array() 是一种选择。内置的 java.util.zip.CRC32 非常慢并且需要重新实现java 中的它是向性能迈出的一个很好的一步。
com.jcraft.jzlib
提供纯java实现(不过看起来几乎像纯C),它是可以用直接缓冲区替换 byte[]。由于边界检查太多并且无法执行如此深度的内联,该库与 inflate(解压缩)相比要慢一些。然而,它的压缩速度更快。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 onlybyte[]
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.