首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将ZipOutputStream转换为ByteArrayInputStream

将ZipOutputStream转换为ByteArrayInputStream
EN

Stack Overflow用户
提问于 2013-11-26 23:23:07
回答 1查看 25.3K关注 0票数 8

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-11-27 17:56:37

我想通了:

代码语言:javascript
复制
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());
}
票数 29
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20221128

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档