PHP: 使用: <? include("class.gzdoc.php"); $pagepress = new gzdoc(); $pagepress->initPage(); //页面输出内容 phpinfo(); $pagepress->sendPage(); ?> class.gzdoc.php --------------------------- <?php Class gzdoc { var $debug = false; var $defaultmethod = 1; var $useZIP = false; var $useDeflate = false; function initPage() { global $HTTP_SERVER_VARS; $encoding = $HTTP_SERVER_VARS["HTTP_ACCEPT_ENCODING"]; if ( strstr($encoding, "deflate") ) { $this->useDeflate = true; } if ( strstr($encoding, "gzip") ) { $this->useZIP = true; } ob_start(); ob_implicit_flush(0); } function sendPage() { $content = ob_get_contents(); ob_end_clean(); if ($this->useDeflate && $this->useZIP) { if ($this->defaultmethod == 1) { $this->useZIP= false; } else { $this->useDeflate = false; } } $lenUnCompressed = strlen($content); if ($this->useDeflate) { $head = "deflate"; $content = gzdeflate($content,9); } if ($this->useZIP) { $head = "gzip"; //$content .= 'Gzip enabled '; $content .= '<div align="center">Gzip 压缩前:' . $lenUnCompressed . '字节'; $content .= ' 压缩后:' . strlen(gzcompress($content, 9)) . ' 字节</div>'; $content = "\x1f\x8b\x08\x00\x00\x00\x00\x00" . gzcompress($content, 9); } $lenCompressed = strlen($content); if ($this->debug) { echo '<HTML><BODY style="font-size:9pt;font-family:Arial,helvetical;color:#000000">'; echo "Size in bytes before compression: ".$lenUnCompressed." bytes<BR>"; echo "Size in bytes after compression: ".$lenCompressed." bytes<BR>"; echo "Percentage reduction: ".sprintf("%1.3f%%",100 - (($lenCompressed / $lenUnCompressed) *100) ); echo '</BODY></HTML>'; } else{ if ( !empty($head) ) { header("Content-Encoding: ".$head); } echo $content; } } } ?> 先手册,以后有用的时候用。
代码开头部分即为调用方法: PHP: <?include("class.gzdoc.php");$pagepress = new gzdoc();$pagepress->initPage();//页面输出内容phpinfo();$pagepress->sendPage();?> 后面部分代码单独存为一个页面 class.gzdoc.php 。