Decompress GZip string in Java

前端 未结 3 2043
北海茫月
北海茫月 2021-02-07 12:40

I can find plenty of functions that let you decompress a GZip file, but how do I decompress a GZip string?

I\'m trying to parse a HTTP response where the response body i

3条回答
  •  北恋
    北恋 (楼主)
    2021-02-07 13:05

    There's no such thing as a GZip string. GZip is binary, strings are text.

    If you want to compress a string, you need to convert it into binary first - e.g. with OutputStreamWriter chained to a compressing OutputStream (e.g. a GZIPOutputStream)

    Likewise to read the data, you can use an InputStreamReader chained to a decompressing InputStream (e.g. a GZIPInputStream).

    One way of easily reading from a Reader is to use CharStreams.toString(Readable) from Guava, or a similar library.

提交回复
热议问题