1. 论坛系统升级为Xenforo,欢迎大家测试!
    排除公告

在线Gzip压缩类

本帖由 小叶2005-11-19 发布。版面名称:后端开发

  1. 小叶

    小叶 New Member

    注册:
    2005-09-04
    帖子:
    17,941
    赞:
    33
    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->useZIPfalse;
                            } 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($content9)) . ' 字节</div>';
                            
    $content "\x1f\x8b\x08\x00\x00\x00\x00\x00" gzcompress($content9);
                    }

                    
    $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;
                    }

            }
    }

    ?>
    先手册,以后有用的时候用。
     
  2. 老林

    老林 New Member

    注册:
    2005-09-06
    帖子:
    10,580
    赞:
    36
    谢谢小叶
     
  3. jumzhou

    jumzhou New Member

    注册:
    2005-11-08
    帖子:
    613
    赞:
    1
    直接用那PHPZIP不是很实际
     
  4. 风吹裤裆pp凉

    风吹裤裆pp凉 New Member

    注册:
    2006-01-01
    帖子:
    5
    赞:
    0
  5. 不学无术

    不学无术 Ulysses 的元神

    注册:
    2005-08-31
    帖子:
    16,714
    赞:
    39
    代码开头部分即为调用方法:

    PHP:
    <?
    include(
    "class.gzdoc.php");
    $pagepress = new gzdoc();
    $pagepress->initPage();
    //页面输出内容
    phpinfo();
    $pagepress->sendPage();
    ?> 
    后面部分代码单独存为一个页面 class.gzdoc.php 。