使用gzip压缩InputStream

27

我想在Java中使用Gzip压缩来压缩输入流。

假设我们有一个未经压缩的输入流(1GB的数据...)。 我想从源获取一个压缩的输入流作为结果:

public InputStream getCompressedStream(InputStream unCompressedStream) {

    // Not working because it's uncompressing the stream, I want the opposite.
    return new GZIPInputStream(unCompressedStream); 

}
12个回答

-1
public InputStream getCompressed( InputStream is ) throws IOException
{
    byte data[] = new byte[2048];
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    GzipOutputStream zos = new GzipOutputStream( bos );
    BufferedInputStream entryStream = new BufferedInputStream( is, 2048);
    int count;
    while ( ( count = entryStream.read( data, 0, 2048) ) != -1 )
    {
        zos.write( data, 0, count );
    }
    entryStream.close();
    zos.close();

    return new ByteArrayInputStream( bos.toByteArray() );
}

参考资料:压缩文件


-3

4
GzipCompressorInputStream 的解压缩方式与 JRE GZIPInputStream 相同。 - PJ Fanning

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接