Java: error creating GZIPInputStream: not in GZIP format

I am trying to use the following Java code to compress and decompress a String. But the line that creates the new GZipInputStream object from the new ByteArrayInputStream object throws an exception "java.util.zip.ZipException: Not in GZIP format". Does anyone know how to solve this?

String orig = "............."; // compress it ByteArrayOutputStream baostream = new ByteArrayOutputStream(); OutputStream outStream = new GZIPOutputStream(baostream); outStream.write(orig.getBytes()); outStream.close(); String compressedStr = baostream.toString(); // uncompress it InputStream inStream = new GZIPInputStream(new ByteArrayInputStream(compressedStr.getBytes())); ByteArrayOutputStream baoStream2 = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int len; while((len = inStream.read(buffer))>0) baoStream2.write(buffer, 0, len); String uncompressedStr = baoStream2.toString(); 
+6
source share
3 answers

Mixing strings and bytes []; which never fits. And it works only on the same OS with the same encoding. Not every byte [] can be converted to a string, and returning back can give other bytes.

Compressed bytes should not represent a string.

Explicitly set the encoding to getBytes and a new line.

  String orig = "............."; // compress it ByteArrayOutputStream baostream = new ByteArrayOutputStream(); OutputStream outStream = new GZIPOutputStream(baostream); outStream.write(orig.getBytes("UTF-8")); outStream.close(); byte[] compressedBytes = baostream.toByteArray(); // toString not always possible // uncompress it InputStream inStream = new GZIPInputStream( new ByteArrayInputStream(compressedBytes)); ByteArrayOutputStream baoStream2 = new ByteArrayOutputStream(); byte[] buffer = new byte[8192]; int len; while ((len = inStream.read(buffer)) > 0) { baoStream2.write(buffer, 0, len); } String uncompressedStr = baoStream2.toString("UTF-8"); System.out.println("orig: " + orig); System.out.println("unc: " + uncompressedStr); 
+6
source

Joop seems to have a solution there, but I feel like I have to add this: Compression in general, and GZIP in particular, will create a binary stream. You SHOULD NOT try to build a string from this thread - this is WILL .

If you need to switch to plain text representation, look at Base64 encoding, hexadecimal encoding, heck, even simple binary encoding.

In short, String objects are for things that people read. Byte arrays (and much more) are designed to read machines.

+3
source

You have encoded baostream to a string with the default platform encoding, possibly UTF-8. You should use baostream.getBytes () to work with binary data, not strings.

If you insist on a string, use an 8-bit encoding, for example. baostream.toString ("ISO-8859-1") and read it with the same encoding.

0
source

All Articles