当前位置:Gxlcms > PHP教程 > php中gzip对文件或字符串解压缩的例子

php中gzip对文件或字符串解压缩的例子

时间:2021-07-01 10:21:17 帮助过:56人阅读

  1. /**
  2. * php自定义gzdecode解压缩gzip文件
  3. * edit bbs.it-home.org
  4. */
  5. if (!function_exists('gzdecode')) {
  6. function gzdecode ($data) {
  7. $flags = ord(substr($data, 3, 1));
  8. $headerlen = 10;
  9. $extralen = 0;
  10. $filenamelen = 0;
  11. if ($flags & 4) {
  12. $extralen = unpack('v' ,substr($data, 10, 2));
  13. $extralen = $extralen[1];
  14. $headerlen += 2 + $extralen;
  15. }
  16. if ($flags & 8) // Filename
  17. $headerlen = strpos($data, chr(0), $headerlen) + 1;
  18. if ($flags & 16) // Comment
  19. $headerlen = strpos($data, chr(0), $headerlen) + 1;
  20. if ($flags & 2) // CRC at end of file
  21. $headerlen += 2;
  22. $unpacked = @gzinflate(substr($data, $headerlen));
  23. if ($unpacked === FALSE)
  24. $unpacked = $data;
  25. return $unpacked;
  26. }
  27. }
  28. ?>

调用:

  1. $f=@file_get_contents(http://bbs.it-home.org);
  2. echo gzdecode($f);
  3. ?>

人气教程排行