Home  >  Article  >  Backend Development  >  PHP decompression of gzip files or strings example reference_PHP tutorial

PHP decompression of gzip files or strings example reference_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:50:32913browse

In fact, php is very simple to decompress gzip. You can just use the built-in gzdecode function. However, unfortunately, I configured it for a long time and could not support the gzdecode function, so I had to work around it:

Copy code The code is as follows:

if (!function_exists('gzdecode')) {                                                  , 3, 1));                                                                            4) {                                                                                                                  'v' ,substr($data, 10, 2));                                                              
if ($flags & 8) // Filename
$headerlen = strpos($data, chr(0), $headerlen) + 1;
if ($flags & 16) // Comment
$headerlen = strpos($ data, chr(0), $headerlen) + 1;
if ($flags & 2) // CRC at end of file
$headerlen += 2;
$unpacked = @ gzinflate(substr( $data, $headerlen));
if ($unpacked === FALSE)
$unpacked = $data;
return $unpacked;

}


The calling method is very simple:



Copy code

The code is as follows:

$f=@file_get_contents("http:/ /www.jb51.net"); 
echo gzdecode($f); 
http://www.bkjia.com/PHPjc/319369.html
www.bkjia.com

true
http: //www.bkjia.com/PHPjc/319369.html

TechArticle

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn