将ZipOutputStream转换为ByteArrayInputStream [英] Convert ZipOutputStream to ByteArrayInputStream

查看:1351
本文介绍了将ZipOutputStream转换为ByteArrayInputStream的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 ZipOutputStream 压缩 InputStream ,然后获取 InputStream ZipOutputStream 中的code>,而无需将文件保存在光盘上。

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

推荐答案

我知道了:

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

这篇关于将ZipOutputStream转换为ByteArrayInputStream的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆