php: string compression tool

Recently I am working on a data export management tool, which is useful for string compression, so I recorded it and hope it will bring convenience to others.

1. What are the compression functions?

gzcompress: Compress the given string using the ZLIB data format

gzencode: Create a string gzip compressed

gzdeflate: shrink a string

bzcompress: Compress a string into bzip2 encoded data

Efficiency comparison:

    gzcompress is the fastest and has a higher compression ratio.
    gzdeflate has the highest compression ratio and is slightly slower than gzcompress.
    gzencode is close to gzdeflate, but gzdeflate has a slight advantage
    . bzcompress has the slowest speed and compression ratio.

Note: It is worth mentioning that gzencode can output a compression compatible with gzip format. If output on a browser, it can be consistent with the GZIP encoding on the HTTP protocol. You can use header('Content-Encoding: gzip'); directly Output without decompression.

2. demo

<?php
/**
 * 压缩工具
 *
 * ===================================================
 * 
 *   gzcompress 速度最快,压缩比率较高。 
 *   gzdeflate  压缩比率最高,速度稍慢于gzcompress 
 *   gzencode 与 gzdeflate 比较接近,gzdeflate稍有优势 
 *   bzcompress 速度最慢,压缩比率最慢。 
 *   
 *   因此建议使用 gzcompress 和 gzdeflate
 *
 * ===================================================
 * 
 * @package Tool
 * @author 蝶开三月
 */
class Tool_Compress {

    /**
     * 压缩(gzcompress)
     * 
     * @param  string  $str   要压缩的字符串
     * @param  int     $level 压缩级别。可以指定为0(表示无压缩),指定为9(表示最大压缩)
     * @return string
     */
    public static function funcGzcompress($str, $level = 9){
        return gzcompress($str, $level);
    }

    /**
     * 解压(gzuncompress)
     *
     * @param  string  $str   要解压的字符串
     * @return string
     */
    public static function funcGzuncompress($str){
        return gzuncompress($str);
    }

    /**
     * 压缩(gzdeflate)
     * 
     * @param  string  $str   要压缩的字符串
     * @param  int     $level 压缩级别。可以指定为0(表示无压缩),指定为9(表示最大压缩)
     * @return string
     */
    public static function funcGzdeflate($str, $level = 9){
        return gzdeflate($str, $level);
    }

    /**
     * 解压(gzinflate)
     *
     * @param  string  $str   要解压的字符串
     * @return string
     */
    public static function funcGzinflate($str){
        return gzinflate($str);
    }

    /**
     * 压缩(gzencode)
     * 因为是与gzip程序输出兼容的压缩版本,可以与HTTP协议上的GZIP编码契合,可以使用header('Content-Encoding: gzip');直接输出,而不用解压
     * 
     * @param  string  $str   要压缩的字符串
     * @param  int     $level 压缩级别。可以指定为0(表示无压缩),指定为9(表示最大压缩)
     * @return string
     */
    public static function funcGzencode($str, $level = 9){
        return gzencode($str, $level);
    }

    /**
     * 解压(gzdecode)
     *
     * @param  string  $str   要解压的字符串
     * @return string
     */
    public static function funcGzdecode($str){
        return gzdecode($str);
    }

    /**
     * 压缩(bzcompress)
     * 
     * @param  string  $str   要压缩的字符串
     * @param  int     $level 压缩级别。可以指定为0(表示无压缩),指定为9(表示最大压缩)
     * @return string
     */
    public static function funcBzcompress($str, $level = 9){
        return bzcompress($str, $level);
    }

    /**
     * 解压(bzdecompress)
     *
     * @param  string  $str   要解压的字符串
     * @return string
     */
    public static function funcBzdecompress($str){
        return bzdecompress($str);
    }    
}

Guess you like

Origin blog.csdn.net/panjiapengfly/article/details/126914559