将ZipOutputStream转换为ByteArrayInputStream

时间:2013-11-26 15:23:07

标签: compression zipoutputstream

我想使用InputStream压缩ZipOutputStream,然后从压缩InputStream获取ZipOutputStream而不保存光盘上的文件。这可能吗?

1 个答案:

答案 0 :(得分:16)

我明白了:

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

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