The correct function for gzip and chunked data particularly when you get "Content-Encoding: gzip" and "Transfer-Encoding: chunked" headers:
<?php
function decode_gzip($h,$d,$rn="\r\n"){
if (isset($h['Transfer-Encoding'])){
$lrn = strlen($rn);
$str = '';
$ofs=0;
do{
$p = strpos($d,$rn,$ofs);
$len = hexdec(substr($d,$ofs,$p-$ofs));
$str .= substr($d,$p+$lrn,$len);
$ofs = $p+$lrn*2+$len;
}while ($d[$ofs]!=='0');
$d=$str;
}
if (isset($h['Content-Encoding'])) $d = gzinflate(substr($d,10));
return $d;
}
?>
Enjoy!
gzinflate
(PHP 4 >= 4.0.4, PHP 5)
gzinflate — deflate圧縮された文字列を解凍する
説明
string gzinflate
( string $data
[, int $length = 0
] )
この関数は収縮された文字列を伸長します。
返り値
オリジナルの無圧縮なデータ、もしくはエラー時に FALSE
この関数は、もし無圧縮なデータが圧縮された入力 data の 32768 倍、もしくはオプションのパラメータ length 以上の場合、エラーを返します。
例
例1 gzinflate() の例
<?php
$compressed = gzdeflate('Compress me', 9);
$uncompressed = gzinflate($compressed);
echo $uncompressed;
?>
参考
- gzdeflate() - 文字列を deflate 圧縮する
- gzcompress() - 文字列を圧縮する
- gzuncompress() - 圧縮された文字列を解凍する
- gzencode() - gzip 圧縮された文字列を作成する
gzinflate
vitall at ua dot fm
10-Sep-2009 09:10
10-Sep-2009 09:10
John
13-Jun-2008 02:22
13-Jun-2008 02:22
And when retrieving mod_deflate gzip'ed content and using gzinflate() to decode the data, be sure to strip the first 11 chars from the retrieved content.
<?php $dec = gzinflate(substr($enc,11)); ?>
patatraboum at free dot fr
25-Aug-2007 02:57
25-Aug-2007 02:57
Some gz string strip header and return inflated
It actualy processes some first member of the gz
See rfc1952 at http://www.faqs.org/rfcs/rfc1952.html for more details and improvment as gzdecode
<?php
function gzBody($gzData){
if(substr($gzData,0,3)=="\x1f\x8b\x08"){
$i=10;
$flg=ord(substr($gzData,3,1));
if($flg>0){
if($flg&4){
list($xlen)=unpack('v',substr($gzData,$i,2));
$i=$i+2+$xlen;
}
if($flg&8) $i=strpos($gzData,"\0",$i)+1;
if($flg&16) $i=strpos($gzData,"\0",$i)+1;
if($flg&2) $i=$i+2;
}
return gzinflate(substr($gzData,$i,-8));
}
else return false;
}
?>
spikeles_ at hotmail dot com
02-Nov-2006 01:12
02-Nov-2006 01:12
This can be used to inflate streams compressed by the Java class java.util.zip.Deflater but you must strip the first 2 bytes off it. ( much like the above comment )
<?php $result = gzinflate(substr($compressedData, 2)); ?>
boris at gamate dot com
08-Jul-2003 09:49
08-Jul-2003 09:49
When retrieving mod_gzip'ed content and using gzinflate() to decode the data, be sure to strip the first 10 chars from the retrieved content.
<?php $dec = gzinflate(substr($enc,10)); ?>
