首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >关闭GZIPOutputStream后,ByteStream复制在最后块中断压缩

关闭GZIPOutputStream后,ByteStream复制在最后块中断压缩
EN

Stack Overflow用户
提问于 2019-04-22 06:48:08
回答 1查看 980关注 0票数 0

我有Zip和解压缩文本的密码。我正面临着奇怪的行为--只有当我在特定的地方关闭GZIPOutputStream时,代码才能工作,但是当我试图将GZIPOutputStream close放在finally块中时,它就会中断而不能工作。如果将:gzipoutputstream.close()放在放置注释的czip函数中,它将工作。但是,如果您只将它保存在finally块中,它就会崩溃。为什么?

ByteStreams.copy函数来自com.google.common.io guava

代码语言:javascript
复制
public class Main {

    public static byte[] dzip(byte[] s) throws IOException {
        ByteArrayInputStream sStream = null;
        ByteArrayOutputStream oStream = null;
        InputStream gzipoutputStream = null;
        ByteBuffer arrReturn = null;
        try {
            sStream = new ByteArrayInputStream(s);
            oStream = new ByteArrayOutputStream();
            gzipoutputStream = new GZIPInputStream(sStream, 1024);
            ByteStreams.copy(gzipoutputStream, oStream);
            arrReturn = ByteBuffer.wrap(oStream.toByteArray());
        }
        catch (Exception e) {
            return null;
        } finally {
            if (gzipoutputStream != null) {
                gzipoutputStream.close();
            }
            if (oStream != null) {
                oStream.close();
            }
            if (sStream != null) {
                sStream.close();
            }

        }
        return arrReturn.array();
    }
    public static byte[] czip(byte[] s) throws IOException {

        ByteArrayInputStream sStream =null;
        ByteArrayOutputStream oStream =null;
        OutputStream gzipoutputstream = null;
        byte[] returnValue = null;

        try {
            sStream = new ByteArrayInputStream(s);
            oStream = new ByteArrayOutputStream(s.length / 2);
            gzipoutputstream = new GZIPOutputStream(oStream, 1024);
            ByteStreams.copy(sStream, gzipoutputstream);

            ////////////   ------------------------------------ \\\\\\\\\\\\
            //gzipoutputstream.close(); // < --- Works only when placed here
            ////////////   ------------------------------------   \\\\\\\\\\\\

            returnValue = oStream.toByteArray();  // Here the zip is 000

        }catch(Exception e) {
            return null;
        } finally {
            if (gzipoutputstream != null) {
                gzipoutputstream.close();
            }
            if (oStream != null) {
                oStream.close();
            }
            if (sStream != null) {
                sStream.close();
            }
        }
        return returnValue;
    }
    public static void main(String[] args) throws IOException {
        String s = "12313dsfafafafaf";
        byte[] source = (byte[]) s.getBytes();
        byte[] returnZipByteArr = czip(source);
        byte[] dd =  dzip(returnZipByteArr);
        String ss = new String(dd);
        System.out.println(ss);

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-04-22 08:59:03

这是意料之中的。close()的javadoc说:

将剩余的压缩数据写入输出流并关闭基础流。

因此,如果您试图在close()调用之前访问ByteArrayOutputStream()中的字节,那么gzip压缩还没有完成: GZIP流需要知道,不会再写任何东西来正确地写入剩余的数据。

您可以调用finish()具有相同的效果,但不需要关闭流(因此仍然在finally块中关闭它)。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55790261

复制
相关文章

相似问题

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