Uncompressing a GZIP string in PHP that was compressed in Java


I have be racking my brain and looking all over to try to uncompress a string that was compressed in Java using GZIP. The compression code looks like this:

public void setBody(String aBody) throws IOException {
		cachedBody = null;
		body = null;
		compressedBody = null;
		if (aBody == null) return;
		ByteArrayOutputStream os = new ByteArrayOutputStream();
		BufferedOutputStream bufos = new BufferedOutputStream(new GZIPOutputStream(os));
    compressedBody = os.toByteArray();

Open in new window

With compressedBody being the final outcome with a string that looks something like this (I only included a partial example of a real string):


The uncompression in the Java code looks like this:

 public String getBody() throws IOException {
		if (compressedBody == null && body == null) return null;
		if (body != null) return body;
		if (cachedBody != null) return cachedBody;
  	ByteArrayInputStream bytes = new ByteArrayInputStream(compressedBody);
   	BufferedInputStream bufis = new BufferedInputStream(new GZIPInputStream(bytes));
   	cachedBody = IOUtils.toString(bufis, "UTF-8"); // StreamHelper.asString(bufis, "UTF-8");
   	return cachedBody;

Open in new window

In PHP, I have tried gzdecode and gzuncompress to not avail. I have read in some places that the gz functions in PHP use zlib and not the actual gzip, which are two different compressions, so these functions would not work. Any help would be great! Thanks.
Christian GaviriaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Hello, Christian Gaviria, and welcome to E-E!

Please post an example of (1) a clear text string and (2) the corresponding GZIP compressed string.  It doesn't have to be very much - just a line or two of data.  We just need to be able to create the SSCCE so we can do some experimentation and testing.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Christian GaviriaAuthor Commented:
Hello Ray,

Attached is a sample XML and the corresponding GZIP we are expecting. In PHP, I should be able to decompress that gzip to the actual XML. Thanks!
Christian GaviriaAuthor Commented:
Hey Ray, I was actually able to resolve the issue on my own, but I appreciate your response, so I will grant your response as the solution. But to explain what happen, the reason gzdecode wasn't working was because the hex values stored in the db are prepended with "0x". Why it is.... I have no idea, since its being stored from another system I have no access to. Anyways, by stripping out that 0x, then simply doing a hex2bin() on that string, then a gzdecode() on that result, I get my XML data. :-)
Ray PaseurCommented:
Glad you got it working.  There is usually some time lag here at E-E because we are all volunteers, so I did not see your last post until just now.  

PHP notation for hex values uses the "0x" prepended to the value.

I'll take a look at the test data in another hour or so, and if I find an "interesting" solution, I'll post it here.  Thanks for the points and best of luck with your project, ~Ray
Ray PaseurCommented:
This seems to work.  Comments and references are in the doc-blocks.
<?php // demo/temp_gaviria.php

 * http://www.experts-exchange.com/questions/28711056/Uncompressing-a-GZIP-string-in-PHP-that-was-compressed-in-Java.html
 * http://php.net/manual/en/function.gzdecode.php
echo '<pre>';

$cleartxt = file_get_contents('http://filedb.experts-exchange.com/incoming/2015/09_w36/933268/qq.txt');
$gzipped  = file_get_contents('http://filedb.experts-exchange.com/incoming/2015/09_w36/933270/qq.gzip.txt');

$gzdecode = gzdecode($gzipped);

echo htmlentities($cleartxt);
echo htmlentities($gzdecode);

if ($gzdecode == $cleartxt) echo PHP_EOL . '&iexcl;SUCCESS!';

Open in new window

Best regards, ~Ray
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.