GZIPInputStream to String

前端 未结 8 1097
北恋
北恋 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:30

    Use Apache Commons to convert GzipInputStream to byteArray.

    import java.io.InputStream;
    import java.util.zip.GZIPInputStream;
    import org.apache.commons.io.IOUtils;
    
    public static byte[] decompressContent(byte[] pByteArray) throws IOException {
            GZIPInputStream gzipIn = null;
            try {
                gzipIn = new GZIPInputStream(new ByteArrayInputStream(pByteArray));
                return IOUtils.toByteArray(gzipIn);
            } finally {
                if (gzipIn != null) {
                    gzipIn.close();
                }
            }
    

    To convert byte array uncompressed content to String, do something like this :

    String uncompressedContent = new String(decompressContent(inputStream));
    

提交回复
热议问题