3

In the many examples online, files are (de)compressed in java using a coded buffer. With NIO, however, there is no need to choose a good buffer size. I found examples for files and sockets, but is there an NIO channel for compressed input (e.g. GZIPInputStream), so you can just use transferFrom in stead of creating the byte[] buffer yourself?

tb189
  • 1,942
  • 3
  • 22
  • 37

2 Answers2

3

No, specialized ZIP channel does not exist yet... I think that you can do the following. Use NIO to read from any channel you want to Buffer. Then retrieve bytes you have just read from buffer to byte array, wrap the array using ByteArrayInputStream and pass it to ZIPInputStream.

AlexR
  • 114,158
  • 16
  • 130
  • 208
3

You can use the static utility methods in java.nio.channels.Channels to wrap streams in channels and vice versa.

E.g. to create a channel, from which you can read the uncompressed data from a gzip compressed file:

FileChannel fc = 
    new RandomAccessFile("input.gz", "r").getChannel();

ReadableByteChannel gzc = 
    Channels.newChannel(
            new GZIPInputStream(
                    Channels.newInputStream(fc)));
jarnbjo
  • 33,923
  • 7
  • 70
  • 94
  • ReadableByteChannel does not have a `transferFrom` method to actually copy the data, and that's what I'm looking for .. – tb189 Aug 26 '11 at 16:19
  • No need to be sarcastic. I edited the question to reflect what I need. – tb189 Sep 02 '11 at 07:33