Home  >  Article  >  Backend Development  >  Example of gzip decompression of files or strings in php

Example of gzip decompression of files or strings in php

WBOY
WBOYOriginal
2016-07-25 08:58:511012browse
  1. /**
  2. * php custom gzdecode to decompress gzip files
  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. ?>
复制代码


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