PHP:gzdeflate 在多台机器上是否安全?

标签 php compression gzip

在 PHP 手册中有一条关于 gzdeflate 的评论说:

gzcompress produces longer data because it embeds information about the encoding onto the string. If you are compressing data that will only ever be handled on one machine, then you don't need to worry about which of these functions you use. However, if you are passing data compressed with these functions to a different machine you should use gzcompress.

然后

running 50000 repetitions on various content, i found that gzdeflate() and gzcompress() both performed equally fast regardless content and compression level, but gzinflate() was always about twice as fast as gzuncompress().

出于我的目的,我将数据存档在一台机器上以备将来使用。数据经常读取,但只写入一次。理论上,如果我在某个时候更换服务器,有一天它会被转移到另一台机器上,但这需要几年的时间。

相对于 gzcompress 和 gzuncompress,我使用 gzdeflate 和 gzinflate 安全吗?

我的想法如下:gzinflate 更快,这将对服务器有很大帮助,因为会有很多读取请求。如果将来某个时候我无法读取文件,那么我应该能够弄清楚如何解压缩文件并重新压缩它,对吧?这并不是说 gzinflate 有一天会神奇地不起作用,就像第一条评论似乎在说的那样。即使缺少 6 字节的 header ,我也确信它可以以某种方式扩展。

想法?

更新 -- 基准

每次 10,000 次迭代:

gzdeflate took 19.158888816833 seconds and size 18521
gzinflate took 1.4803981781006 seconds
gzcompress took 19.376484870911 seconds and size 18527
gzuncompress took 1.6339199542999 seconds
gzencode took 20.015944004059 seconds and size 18539
gzdecodetook 1.8822891712189 seconds

最佳答案

评论是废话。您可以使用 gzcompressgzdeflategzencode 中的任何一个来生成可以在任何地方可移植解压缩的压缩数据。这些函数仅在压缩数据 (RFC 1951) 的包装器方面有所不同。 gzcompress 有一个 zlib 包装器(RFC 1950),gzdeflate 没有包装器,gzencode 有一个 gzip 包装器(RFC 1952)。

我建议不要使用gzdeflate,因为没有包装器意味着没有完整性检查。 gzdeflate 只应在外部生成其他包装器时使用,例如对于 zip 文件,它也使用 deflate 格式。关于速度的评论几乎肯定是错误的。与解压缩相比,gzuncompress() 的完整性检查花费的时间非常少。你应该做你自己的测试。

从这个例子来看,我可能过于笼统了,但我会说您应该完全忽略 PHP 文档中的注释。慷慨地说,他们是无知的。

顺便说一下,这些函数的命名方式非常困惑。只有 gzencode 应该在名称中包含“gz”,因为这是唯一一个实际处理 .gz 格式的代码。 gzcompress 听起来像是压缩成gzip格式,但实际上是压缩成zlib格式。

关于PHP:gzdeflate 在多台机器上是否安全?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15022677/

相关文章:

PHP MySQL 选择长度错误

php - 为什么这个 PDO 语句会默默地失败?

php - 如何在android中安排通知并在固定时间发送

algorithm - 排序整数的压缩算法

compression - 解压gz SQL文件

algorithm - 压缩实用程序如何将文件按顺序添加到压缩存档中?

csv - 在 Python 中从 url 下载 csv.gz 文件

iis - 使用压缩动态内容时如何禁用分块传输编码?

php - 如何在 Laravel 中静态使用布局?

android - Android 中的 GZip