Re: Exception in thread "main" java.util.zip.ZipException: Not in
GZIP format
On 10/23/14 3:51 AM, takouarnauld@gmail.com wrote:
Good Morning,
I'm trying in my program to Compress/Decompress data using GZIP streams and when using the charset "ISO-8859-1", everything working well but when changing the charset to "UTF-8", i'm getting the Error message "Exception in thread "main" java.util.zip.ZipException: Not in GZIP format".
this is my code:
public static String compress(String str) throws IOException {
if (str == null || str.length() == 0) {
return str;
}
System.out.println("String length : " + str.length());
ByteArrayOutputStream out = new ByteArrayOutputStream();
GZIPOutputStream gzip = new GZIPOutputStream(out);
gzip.write(str.getBytes());
gzip.close();
String outStr = out.toString("UTF-8");
You have no reason to believe that the byte array output from GZIP is
the UTF-8 encoding of a String. <a
href="http://docs.oracle.com/javase/8/docs/api/java/io/ByteArrayOutputStream.html#toString-java.lang.String-">Java
doc for ByteArrayOutputStream.toString(String encoding)</a> says that
"This method always replaces malformed-input and unmappable-character
sequences with this charset's default replacement string". After that
replacement, you can never recover the original contents of the byte
array from the resultant String.
--Mike Amling
SWYgeW91IHdhbnQgdG8gcmVwcmVzZW50IGFuIGFyYml0cmFyeSBieXRlIGFycmF5IGFzIGEgU3Ry
aW5nLCB1c2UgaGV4IG9yIGJhc2UgNjQu