文字

gzinflate

(PHP 4 >= 4.0.4, PHP 5)

gzinflateInflate a deflated string

说明

string gzinflate ( string $data [, int $length = 0 ] )

This function inflates a deflated string.

参数

data

The data compressed by gzdeflate() .

length

The maximum length of data to decode.

返回值

The original uncompressed data or FALSE on error.

The function will return an error if the uncompressed data is more than 32768 times the length of the compressed input data or more than the optional parameter length.

范例

Example #1 gzinflate() example

<?php
$compressed   
gzdeflate ( 'Compress me' 9 );
$uncompressed  gzinflate ( $compressed );
echo 
$uncompressed ;
?>

参见

  • gzdeflate() - Deflate a string
  • gzcompress() - Compress a string
  • gzuncompress() - Uncompress a compressed string
  • gzencode() - Create a gzip compressed string

用户评论:

[#1] anonymous at dekho-ji dot com [2013-05-16 11:34:46]

To decode / uncompress the received HTTP POST data in PHP code, request data coming from Java / Android application via HTTP POST GZIP / DEFLATE compressed format

1) Data sent from Java Android app to PHP using DeflaterOutputStream java class and received in PHP as shown below
echo gzinflate( substr($HTTP_RAW_POST_DATA,2,-4) ) . PHP_EOL  . PHP_EOL;

2) Data sent from Java Android app to PHP using GZIPOutputStream java class and received in PHP code as shown below
echo gzinflate( substr($HTTP_RAW_POST_DATA,10,-8) ) . PHP_EOL  . PHP_EOL;

From Java Android side (API level 10+), data being sent in DEFLATE compressed format
String body = "Lorem ipsum shizzle ma nizle";
URL url = new URL("http://www.url.com/postthisdata.php");
URLConnection conn = url.openConnection();
conn.setDoOutput(true);
conn.setRequestProperty("Content-encoding", "deflate");
conn.setRequestProperty("Content-type", "application/octet-stream");
DeflaterOutputStream dos = new DeflaterOutputStream(
conn.getOutputStream());
dos.write(body.getBytes());
dos.flush();
dos.close();
BufferedReader in = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String decodedString = "";
while ((decodedString = in.readLine()) != null) {
Log.e("dump",decodedString);
}
in.close();

On PHP side (v 5.3.1), code for decompressing this DEFLATE data will be
echo substr($HTTP_RAW_POST_DATA,2,-4);

From Java Android side (API level 10+), data being sent in GZIP compressed format

String body1 = "Lorem ipsum shizzle ma nizle";
URL url1 = new URL("http://www.url.com/postthisdata.php");
URLConnection conn1 = url1.openConnection();
conn1.setDoOutput(true);
conn1.setRequestProperty("Content-encoding", "gzip");
conn1.setRequestProperty("Content-type", "application/octet-stream");
GZIPOutputStream dos1 = new GZIPOutputStream(conn1.getOutputStream());
dos1.write(body1.getBytes());
dos1.flush();
dos1.close();
BufferedReader in1 = new BufferedReader(new InputStreamReader(
conn1.getInputStream()));
String decodedString1 = "";
while ((decodedString1 = in1.readLine()) != null) {
Log.e("dump",decodedString1);
}
in1.close();

On PHP side (v 5.3.1), code for decompressing this GZIP data will be
echo substr($HTTP_RAW_POST_DATA,10,-8);

Useful PHP code for printing out compressed data using all available formats.

$data = "Lorem ipsum shizzle ma nizle";
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzcompress($data,$i))),2," ") . PHP_EOL  . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzdeflate($data,$i))),2," ") . PHP_EOL  . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_GZIP))),2," ") . PHP_EOL  . PHP_EOL;
echo "\n\n\n";
for($i=-1;$i<=9;$i++)
echo chunk_split(strtoupper(bin2hex(gzencode($data,$i,FORCE_DEFLATE))),2," ") . PHP_EOL  . PHP_EOL;
echo "\n\n\n";

Hope this helps. Please ThumbsUp if this saved you a lot of effort and time.

[#2] felix dot klee at inka dot de [2013-02-27 22:38:49]

The code below illustrates usage of the second parameter, in particular to protect against fatal out-of-memory errors. It outputs:

1000000
1000000
error

Tested with PHP 5.3 on 32bit Linux.

<?php

function tryToGzinflate($deflatedData$maxLen 0) {
  
$data gzinflate($deflatedData$maxLen);
  if (
$data === false)
    echo 
'error<br>';
  else
    echo 
strlen($data).'<br>';
}

// random data:
$data '';
for (
$i 0$i 1000000$i++)
  
$data .= chr(mt_rand(97122)); // a-z

$deflatedData gzdeflate($data);

ini_set('memory_limit''5M'); // plenty of memory
tryToGzinflate($deflatedData);
tryToGzinflate($deflatedDatastrlen($data));

ini_set('memory_limit''100'); // little memory
tryToGzinflate($deflatedData100);
tryToGzinflate($deflatedData); // causes fatal out-of-memory error
?>

[#3] akniep at rayo dot info [2012-12-11 15:11:39]

Take care when using the optional second parameter $length!
In our tests -at least in certain situations- we were unable to determine the actual use of this parameter, plus, it lead to the script either being unable to inflate compressed data or crash due to memory-problems.

Example:
When trying to inflate the compressed data from a website, we were literally unable to find a value (other than 0) for $length in order to make gzinflate work... while without the second parameter (or setting it to 0) gzinflate worked like a charm:

<?php
// -----------------------------------------------------------------------
// Test 1 works without problems. Memory peak usage: Before inflating: 24.787 kB; after: 24.844 kB.
gzinflatesubstr($deflated_body10) );

// -----------------------------------------------------------------------
// ALL three of the following tests failed with a warning. Memory peak usage: Before inflating: 24.787 kB; after: 298.262 kB.
gzinflatesubstr($deflated_body10),       200 strlen($deflated_body) );
gzinflatesubstr($deflated_body10),     32768 strlen($deflated_body) );
gzinflatesubstr($deflated_body10),     11000 );
gzinflatesubstr($deflated_body10), 280000000 );    // the PHP memory limit was set to 300MB  (memory_limit=300M)
=>
Warning:  gzinflate() [function.gzinflate]: insufficient memory in [...]

// -----------------------------------------------------------------------
// The last test failed with a fatal error. Memory peak usage: Before inflating: 24.787 kB; after: ? (> 300M).
gzinflatesubstr($deflated_body10), 300000000 );    // the PHP memory limit was set to 300MB  (memory_limit=300M)
=>
Fatal error:  Allowed memory size of 314572800 bytes exhausted (tried to allocate 300000000 bytesin 
?>


In short: We were unable to determine the actual use of the second parameter in certain situations.
Be careful with using the second parameter $length!

[#4] niblett at gmail dot com [2012-06-07 14:08:11]

alternative, with detection for optional filename header
<?php
function gzdecode($data) {

        
// check if filename field is set in FLG, is 4th byte
        
$hex bin2hex($data);

        
$flg = (int)hexdec(substr($hex62));

        
// remove headers
        
$hex substr($hex20);

        
$ret '';
        if (  (
$flg 0x8) == ){
                print 
"ello";
                for ( 
$i 0$i strlen($hex); $i += ){
                        
$value substr($hex$i2);
                        if ( 
$value == '00' ){
                                
$ret substr($hex, ($i+2));
                                break;
                        }
                }
        }
        return 
gzinflate(pack('H*'$ret));
}
?>

[#5] Steven Lustig [2010-11-05 09:44:20]

You can use this to uncompress a string from Linux command line gzip by stripping the first 10 bytes:

<?php
$inflatedOutput 
gzinflate(substr($output10, -8));
?>

[#6] vitall at ua dot fm [2009-09-09 17:10:19]

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!

[#7] John [2008-06-12 22:22:38]

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)); ?>

[#8] patatraboum at free dot fr [2007-08-24 10:57:03]

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;
}
?>

[#9] spikeles_ at hotmail dot com [2006-11-01 20:12:26]

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($compressedData2)); ?>

[#10] boris at gamate dot com [2003-07-08 05:49:19]

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)); ?>

上一篇: 下一篇: