简体   繁体   English

在Java中读取GZIP文件导致ZLIB输入流意外结束

[英]Reading GZIP File Causing Unexpected end of ZLIB input stream in java

I am converting GZIP byte array to String form in Java.This is considerably large file and idea is to convert this in to JSON. 我正在将Java中的GZIP字节数组转换为String形式。这是一个很大的文件,想法是将其转换为JSON。

But Exception I am getting is quite weird and is not making much Sense. 但是我得到的异常很奇怪并且没有多大意义。

Code Snippeet: 代码段:

public static String convert(byte[] bytes) throws IOException {
        final byte[] BUFFER = new byte[16234];

        GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(bytes));

        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

        int len;
        while ((len = gzipInputStream.read(BUFFER)) >= 0) {
            byteArrayOutputStream.write(BUFFER, 0, len);
          if(byteArrayOutputStream.size ()>60812918){
                System.out.println ( "stopping here" );
            }
        }

        byteArrayOutputStream.flush();
        byteArrayOutputStream.close();

        gzipInputStream.close();

        final byte[] dataPart = byteArrayOutputStream.toByteArray();

        String data = new String(dataPart, StandardCharsets.UTF_8);

        return data;
    }

Exception Trace: 异常跟踪:

java.io.EOFException: Unexpected end of ZLIB input stream
    at java.util.zip.InflaterInputStream.fill(InflaterInputStream.java:240)
    at java.util.zip.InflaterInputStream.read(InflaterInputStream.java:158)
    at java.util.zip.GZIPInputStream.read(GZIPInputStream.java:117)
    at java.io.FilterInputStream.read(FilterInputStream.java:107)
    at com.here.rcs.discoverkernels.testS3FileReader.convert(testS3FileReader.java:84)
    at com.here.rcs.discoverkernels.testS3FileReader.viewJson(testS3FileReader.java:45)
    at com.here.rcs.discoverkernels.testS3FileReader.main(testS3FileReader.java:21)

From Coding point of view,I don't think there is something wrong with this piece of code. 从编码的角度来看,我认为这段代码没有错。

Any Suggestions how to move forward with this. 任何建议如何继续前进。

Adding To Byte Conversion Part; 添加到字节转换部分;

public static byte[] compress(final String data) throws IOException {

    final byte[] dataPart = data.getBytes( StandardCharsets.UTF_8 );

    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

    GZIPOutputStream gzipOutputStream = new GZIPOutputStream(byteArrayOutputStream);
    gzipOutputStream.write(dataPart);

    gzipOutputStream.flush();
    gzipOutputStream.close();

    byte[] bytes = byteArrayOutputStream.toByteArray();

    return bytes;
}

I tried your program with a small .gz file, and it works as expected. 我用一个小的.gz文件尝试了您的程序,它可以按预期工作。 I guess that 我猜是

  1. there are issues handling large files, or, more likely, 处理大型文件时出现问题,或更可能的是,
  2. you did not loaded correctly data from file to bytes array. 您没有将数据正确地从文件加载到bytes数组。 How did you? 你怎么样 I followed this article: https://netjs.blogspot.com/2015/11/how-to-convert-file-to-byte-array-java.html 我关注了这篇文章: https : //netjs.blogspot.com/2015/11/how-to-convert-file-to-byte-array-java.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM