Compress an InputStream with gzip

前端 未结 12 1405
迷失自我
迷失自我 2020-12-29 05:28

I would like to compress an input stream in java using Gzip compression.

Let\'s say we have an input stream (1GB of data..) not compressed. I want as a result a comp

12条回答
  •  时光取名叫无心
    2020-12-29 06:23

    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() );
    }
    

    ref :zip compression

提交回复
热议问题