Java: Error creating a GZIPInputStream: Not in GZIP format

前端 未结 3 2228
暖寄归人
暖寄归人 2020-12-17 23:39

I am trying to use the following Java code to compress and uncompress a String. But the line that creates a new GZipInputStream object out of a new ByteArrayInputStream obj

3条回答
  •  生来不讨喜
    2020-12-18 00:33

    Joop seems to have the solution up there, but I feel I must add this: Compression in general, and GZIP in particular will produce a binary stream. You MUST not try to construct a String from this stream - it WILL break.

    If you need to take it to a plain text representation, look into Base64 encoding, hex encoding, heck, even simple binary encoding.

    In short, String objects are for things that humans read. Byte arrays (and many other things) are for things machines read.

提交回复
热议问题