Convert ZipOutputStream to ByteArrayInputStream

萝らか妹 提交于 2019-12-23 09:30:05

问题


I want to compress an InputStream using ZipOutputStream and then get the InputStream from compressed ZipOutputStream without saving file on disc. Is that possible?


回答1:


I figured it out:

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


来源:https://stackoverflow.com/questions/20221128/convert-zipoutputstream-to-bytearrayinputstream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!