Convert ZipOutputStream to ByteArrayInputStream

后端 未结 1 569
猫巷女王i
猫巷女王i 2021-01-18 06:11

I want to compress an InputStream using ZipOutputStream and then get the InputStream from compressed ZipOutputStream with

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-18 06:42

    I figured it out:

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

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