Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #30165

Re: Exception in thread "main" java.util.zip.ZipException: Not in GZIP format

From Mike Amling <mamling@chaff.us>
Newsgroups comp.lang.java.programmer
Subject Re: Exception in thread "main" java.util.zip.ZipException: Not in GZIP format
Date 2014-10-23 09:30 -0500
Organization A noiseless patient Spider
Message-ID <m2b3d4$e5a$1@dont-email.me> (permalink)
References <38eee3eb-4817-4a14-b823-4609b421b3fa@googlegroups.com>

Show all headers | View raw


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

Back to comp.lang.java.programmer | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

Exception in thread "main" java.util.zip.ZipException: Not in GZIP format takouarnauld@gmail.com - 2014-10-23 01:51 -0700
  Re: Exception in thread "main" java.util.zip.ZipException: Not in GZIP format Martin Gregorie <martin@address-in-sig.invalid> - 2014-10-23 11:19 +0000
  Re: Exception in thread "main" java.util.zip.ZipException: Not in GZIP format Steven Simpson <ss@domain.invalid> - 2014-10-23 14:31 +0100
  Re: Exception in thread "main" java.util.zip.ZipException: Not in GZIP format Mike Amling <mamling@chaff.us> - 2014-10-23 09:30 -0500

csiph-web