Christian Gaviria
asked on
Uncompressing a GZIP string in PHP that was compressed in Java
Hello,
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:
With compressedBody being the final outcome with a string that looks something like this (I only included a partial example of a real string):
"0x1F8B0800000000000000ED5 DE972E3469 2FEEFA7A8E 58F0D7B431 40952176D3 6BC3...... "
The uncompression in the Java code looks like this:
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.
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));
bufos.write(aBody.getBytes("UTF-8"));
bufos.flush();
bufos.close();
os.close();
compressedBody = os.toByteArray();
}
With compressedBody being the final outcome with a string that looks something like this (I only included a partial example of a real string):
"0x1F8B0800000000000000ED5
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;
}
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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. :-)
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.
http://php.net/manual/en/language.types.integer.php
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
PHP notation for hex values uses the "0x" prepended to the value.
http://php.net/manual/en/language.types.integer.php
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
This seems to work. Comments and references are in the doc-blocks.
http://iconoun.com/demo/temp_gaviria.php
http://iconoun.com/demo/temp_gaviria.php
<?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
*/
error_reporting(E_ALL);
mb_internal_encoding('UTF-8');
mb_regex_encoding('UTF-8');
echo '<pre>';
// THE TEST DATA SET
$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');
// DECODE THE GZIPPED VERSION
$gzdecode = gzdecode($gzipped);
// SHOW THE ORIGINAL AND THE DECODED VERSION
echo htmlentities($cleartxt);
echo htmlentities($gzdecode);
// TEST FOR SUCCESS
if ($gzdecode == $cleartxt) echo PHP_EOL . '¡SUCCESS!';
Best regards, ~Ray
ASKER
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!
qq.txt
qq.gzip.txt