首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >正确读取gzip文件

正确读取gzip文件
EN

Stack Overflow用户
提问于 2021-03-07 10:21:06
回答 1查看 21关注 0票数 0
代码语言:javascript
复制
public static byte[] decompressGzipToBytes(String file) throws IOException {

    ByteArrayOutputStream output = new ByteArrayOutputStream();

    FileInputStream in = new FileInputStream(file);
    try (GZIPInputStream gis = new GZIPInputStream(in)) {

        // copy GZIPInputStream to ByteArrayOutputStream
        byte[] buffer = new byte[1024];
        int len;
        while ((len = gis.read(buffer)) > 0) {
            output.write(buffer, 0, len);
            System.out.println(buffer);
        }

    }

    return output.toByteArray();

}

上面的代码不能正确地将Gzip文件读成字节。我得到像'[B@6d6f6e28‘这样的字符。但是,如果我对文本文件使用BufferReader,那么它的行为就像预期的那样。

我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2021-03-07 10:29:10

如果有效,可以像下面这样尝试

代码语言:javascript
复制
GZIPInputStream gzip = new GZIPInputStream(new FileInputStream(filePath));
BufferedReader br = new BufferedReader(new InputStreamReader(gzip));
br.readLine();
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66512636

复制
相关文章

相似问题

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