(de)compressing files using NIO

后端 未结 2 1453
执念已碎
执念已碎 2021-01-14 12:09

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 fi

相关标签:
2条回答
  • 2021-01-14 12:29

    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)));
    
    0 讨论(0)
  • 2021-01-14 12:31

    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.

    0 讨论(0)
提交回复
热议问题