GZIPInputStream to String

前端 未结 8 1111
北恋
北恋 2020-12-04 21:49

I am attempting to convert the gzipped body of a HTTP response to plaintext. I\'ve taken the byte array of this response and converted it to a ByteArrayInputStream. I\'ve th

8条回答
  •  醉话见心
    2020-12-04 22:36

    To decode bytes from an InputStream, you can use an InputStreamReader. Then, a BufferedReader will allow you to read your stream line by line.

    Your code will look like:

    ByteArrayInputStream bais = new ByteArrayInputStream(responseBytes);
    GZIPInputStream gzis = new GZIPInputStream(bais);
    InputStreamReader reader = new InputStreamReader(gzis);
    BufferedReader in = new BufferedReader(reader);
    
    String readed;
    while ((readed = in.readLine()) != null) {
        System.out.println(readed);
    }
    

提交回复
热议问题