设为首页收藏本站
开启辅助访问
切换到宽版

创星网络[分享知识 传递快乐]

 找回密码
 立即注册

QQ登录

只需一步,快速开始

用新浪微博登录

只需一步,快速搞定

搜索
查看: 4534|回复: 0
打印 上一主题 下一主题

PHP中实现GZIP解压缩函数

[复制链接]

我玩的应用:

跳转到指定楼层
楼主
发表于 2012-10-20 00:10:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. function gzdecode($data) {
  2.   $len = strlen($data);
  3.   if ($len < 18 || strcmp(substr($data,0,2),\"x1fx8b\")) {
  4.    return null;  // Not GZIP format (See RFC 1952)
  5.   }
  6.   $method = ord(substr($data,2,1));  // Compression method
  7.   $flags  = ord(substr($data,3,1));  // Flags
  8.   if ($flags & 31 != $flags) {
  9.    // Reserved bits are set -- NOT ALLOWED by RFC 1952
  10.    return null;
  11.   }
  12.   // NOTE: $mtime may be negative (PHP integer limitations)
  13.   $mtime = unpack(\"V\", substr($data,4,4));
  14.   $mtime = $mtime[1];
  15.   $xfl  = substr($data,8,1);
  16.   $os    = substr($data,8,1);
  17.   $headerlen = 10;
  18.   $extralen  = 0;
  19.   $extra    = \"\";
  20.   if ($flags & 4) {
  21.    // 2-byte length prefixed EXTRA data in header
  22.    if ($len - $headerlen - 2 < 8) {
  23.      return false;    // Invalid format
  24.    }
  25.    $extralen = unpack(\"v\",substr($data,8,2));
  26.    $extralen = $extralen[1];
  27.    if ($len - $headerlen - 2 - $extralen < 8) {
  28.      return false;    // Invalid format
  29.    }
  30.    $extra = substr($data,10,$extralen);
  31.    $headerlen += 2 + $extralen;
  32.   }
  33.   $filenamelen = 0;
  34.   $filename = \"\";
  35.   if ($flags & 8) {
  36.    // C-style string file NAME data in header
  37.    if ($len - $headerlen - 1 < 8) {
  38.      return false;    // Invalid format
  39.    }
  40.    $filenamelen = strpos(substr($data,8+$extralen),chr(0));
  41.    if ($filenamelen === false || $len - $headerlen - $filenamelen - 1 < 8) {
  42.      return false;    // Invalid format
  43.    }
  44.    $filename = substr($data,$headerlen,$filenamelen);
  45.    $headerlen += $filenamelen + 1;
  46.   }
  47.   $commentlen = 0;
  48.   $comment = \"\";
  49.   if ($flags & 16) {
  50.    // C-style string COMMENT data in header
  51.    if ($len - $headerlen - 1 < 8) {
  52.      return false;    // Invalid format
  53.    }
  54.    $commentlen = strpos(substr($data,8+$extralen+$filenamelen),chr(0));
  55.    if ($commentlen === false || $len - $headerlen - $commentlen - 1 < 8) {
  56.      return false;    // Invalid header format
  57.    }
  58.    $comment = substr($data,$headerlen,$commentlen);
  59.    $headerlen += $commentlen + 1;
  60.   }
  61.   $headercrc = \"\";
  62.   if ($flags & 1) {
  63.    // 2-bytes (lowest order) of CRC32 on header present
  64.    if ($len - $headerlen - 2 < 8) {
  65.      return false;    // Invalid format
  66.    }
  67.    $calccrc = crc32(substr($data,0,$headerlen)) & 0xffff;
  68.    $headercrc = unpack(\"v\", substr($data,$headerlen,2));
  69.    $headercrc = $headercrc[1];
  70.    if ($headercrc != $calccrc) {
  71.      return false;    // Bad header CRC
  72.    }
  73.    $headerlen += 2;
  74.   }
  75.   // GZIP FOOTER - These be negative due to PHP\'s limitations
  76.   $datacrc = unpack(\"V\",substr($data,-8,4));
  77.   $datacrc = $datacrc[1];
  78.   $isize = unpack(\"V\",substr($data,-4));
  79.   $isize = $isize[1];
  80.   // Perform the decompression:
  81.   $bodylen = $len-$headerlen-8;
  82.   if ($bodylen < 1) {
  83.    // This should never happen - IMPLEMENTATION BUG!
  84.    return null;
  85.   }
  86.   $body = substr($data,$headerlen,$bodylen);
  87.   $data = \"\";
  88.   if ($bodylen > 0) {
  89.    switch ($method) {
  90.      case 8:
  91.        // Currently the only supported compression method:
  92.        $data = gzinflate($body);
  93.        break;
  94.      default:
  95.        // Unknown compression method
  96.        return false;
  97.    }
  98.   } else {
  99.    // I\'m not sure if zero-byte body content is allowed.
  100.    // Allow it for now...  Do nothing...
  101.   }
  102.   // Verifiy decompressed size and CRC32:
  103.   // NOTE: This may fail with large data sizes depending on how
  104.   //      PHP\'s integer limitations affect strlen() since $isize
  105.   //      may be negative for large sizes.
  106.   if ($isize != strlen($data) || crc32($data) != $datacrc) {
  107.    // Bad format!  Length or CRC doesn\'t match!
  108.    return false;
  109.   }
  110.   return $data;
  111. }
复制代码


  from:http://nakupanda.iteye.com/blog/441234
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 转播转播 分享分享 分享淘帖
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|创星网络 ( 苏ICP备11027519号|网站地图  

GMT+8, 2024-9-22 06:47 , Processed in 0.081104 second(s), 25 queries .

Powered by Discuz! X3

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表