Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!news2.arglkargh.de!news.swapon.de!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Mike Amling Newsgroups: comp.lang.java.programmer Subject: Re: Exception in thread "main" java.util.zip.ZipException: Not in GZIP format Date: Thu, 23 Oct 2014 09:30:46 -0500 Organization: A noiseless patient Spider Lines: 29 Message-ID: References: <38eee3eb-4817-4a14-b823-4609b421b3fa@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Thu, 23 Oct 2014 14:29:56 +0000 (UTC) Injection-Info: mx02.eternal-september.org; posting-host="776bc2d2547a00965cb14ddc8a2ce3c1"; logging-data="14506"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19tGwu8WS0A9DjSJB5cbBAD" User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:24.0) Gecko/20100101 Thunderbird/24.6.0 In-Reply-To: <38eee3eb-4817-4a14-b823-4609b421b3fa@googlegroups.com> Cancel-Lock: sha1:O88nlbMfCK8XB8pSzZV7yb7tkIU= Xref: csiph.com comp.lang.java.programmer:30165 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. Java doc for ByteArrayOutputStream.toString(String encoding) 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