<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; 压缩函数</title>
	<atom:link href="https://www.phppan.com/tag/%e5%8e%8b%e7%bc%a9%e5%87%bd%e6%95%b0/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>PHP的压缩函数实现：gzencode、gzdeflate和gzcompress</title>
		<link>https://www.phppan.com/2014/03/php-compress/</link>
		<comments>https://www.phppan.com/2014/03/php-compress/#comments</comments>
		<pubDate>Sun, 16 Mar 2014 08:55:19 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[PHP源码阅读笔记]]></category>
		<category><![CDATA[压缩函数]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1902</guid>
		<description><![CDATA[gzencode 默认使用ZLIB_ENCODING_GZIP编码，使用gzip压缩格式，实际上是使用defa [&#8230;]]]></description>
				<content:encoded><![CDATA[<ul>
<li>
gzencode 默认使用ZLIB_ENCODING_GZIP编码，使用gzip压缩格式，实际上是使用defalte 算法压缩数据，然后加上文件头和adler32校验</li>
<li>
gzdeflate  默认使用ZLIB_ENCODING_RAW编码方式，使用deflate数据压缩算法，实际上是先用 LZ77 压缩，然后用霍夫曼编码压缩</li>
<li>
gzcompress ；默认使用ZLIB_ENCODING_DEFLATE编码，使用zlib压缩格式，实际上是用 deflate 压缩数据，然后加上 zlib 头和 CRC 校验</li>
<p><strong>这三个函数的比较实质上是三种压缩方法：deflate, zlib, gzip的比较。</strong><br />
从性能的维度看：deflate 好于 gzip 好于 zlib<br />
从文本文件默认压缩率压缩后体积的维度看：deflate 好于 zlib 好于 gzip</p>
<p>这三种算法中gzip 、zlib的作者都是Jean-Loup Gailly和 Mark Adler。<br />
这两种算法以及图形格式png，使用的压缩算法却都是deflate算法。<br />
deflate算法是同时使用了LZ77算法与哈夫曼编码（Huffman Coding）的一个无损数据压缩算法。<br />
它最初是由Phil Katz为他的PKZIP归档工具第二版所定义的，后来定义在 RFC 1951规范中。</p>
<p>deflate算法的压缩与解压的实现过程可以在压缩库zlib上找到。<br />
PHP的压缩实现依赖于zlib，zlib是一个提供了 deflate, zlib, gzip 压缩方法的函数库。<br />
我们所使用的上面三个函数，将参数中的encoding转为相同，压缩率设置相同，则其最终调用的是同一个函数，效果和性能一样。</p>
<p>PHP的zlib实现是以扩展的方式存在于ext/zlib目录中。通过deflateInit2() + deflate() + deflateEnd()三个函数配合完成压缩功能，inflateInit2() + inflate() + inflateEnd()三个函数配合完成解压功能。压缩最终都是通过php_zlib_encode函数实现调用，除了输入的字符串，压缩率，结果的输出外，不同的入口函数调用参数不同的是其encoding。deflateInit2的第四个参数指定encoding，PHP定义了三个常量：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="c" style="font-family:monospace;"> <span style="color: #339933;">#define PHP_ZLIB_ENCODING_RAW          -0xf      //deflate -15</span>
<span style="color: #339933;">#define PHP_ZLIB_ENCODING_GZIP          0x1f      //gzip 15 + 16</span>
<span style="color: #339933;">#define PHP_ZLIB_ENCODING_DEFLATE     0x0f      // zlib 15</span></pre></td></tr></table></div>

<p>三个函数在调用过程可以直接指定encoding使用其它的算法：</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="shell" style="font-family:monospace;">zlib:   ZLIB_ENCODING_DEFLATE 
gzip: ZLIB_ENCODING_GZIP
deflate: ZLIB_ENCODING_RAW</pre></td></tr></table></div>

<p>此三个函数是三种算法的简单调用方式，以更好的命名展现。三个函数间可以通过指定相同的encoding达到相同的效果，并且PHP也提供zlib_encode函数作为通用的压缩函数。</p>
<p>参考资料：</p>
<p>http://www.gzip.org/zlib/rfc-deflate.html</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2014/03/php-compress/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
