用stream_filter_append和stream_copy_to_stream解压缩 gzip


uncompressing gzip with stream_filter_append and stream_copy_to_stream

找到这个:https://stackoverflow.com/a/11373078/530599 - 很棒,但是

stream_filter_append($fp, 'zlib.inflate', STREAM_FILTER_*怎么样

寻找另一种解压缩数据的方法。

$fp = fopen($src, 'rb');
$to = fopen($output, 'wb');
// some filtering here?
stream_copy_to_stream($fp, $to);
fclose($fp);
fclose($to);

其中$src是要http://.../file.gz的一些网址,例如 200+ Mb :)

添加了有效的测试代码,但分为 2 个步骤:

<?php
    $src = 'http://is.auto.ru/catalog/catalog.xml.gz';
    $fp = fopen($src, 'rb');
    $to = fopen(dirname(__FILE__) . '/output.txt.gz', 'wb');
    stream_copy_to_stream($fp, $to);
    fclose($fp);
    fclose($to);
    copy('compress.zlib://' . dirname(__FILE__) . '/output.txt.gz', dirname(__FILE__) . '/output.txt');

尝试打开 gzip (.gz) 文件进行读取或写入的gzopen。 如果文件未压缩,它将透明地读取它,以便您可以安全地读取非 gzip 文件。

$fp = gzopen($src, 'rb');
$to = fopen($output, 'w+b');
while (!feof($fp)) {
    fwrite($to, gzread($fp, 2048)); // writes decompressed data from $fp to $to
}
fclose($fp);
fclose($to);
PHP 的

流过滤器子系统中一个令人讨厌的遗漏是缺少 gzip 过滤器。Gzip 本质上是使用 Deflate 方法压缩的内容。但是,它在放气的数据之前添加一个 2 字节标头,并在末尾添加一个 Adler-32 校验和。如果你只是将 zlib.inflate 过滤器添加到流中,它将无法正常工作。在附加过滤器之前,您必须跳过前两个字节。

请注意,PHP 版本 5.2.X 中的流过滤器存在一个严重的错误。这是由于流缓冲。基本上,PHP 将无法通过过滤器传递流内部缓冲区中已有的数据。如果您在连接膨胀过滤器之前执行 fread($handle, 2) 来读取 gzip 标头,则很有可能会失败。调用 fread() 会导致 PHP 尝试填满其缓冲区。即使对 fread() 的调用只要求两个字节,PHP 实际上也可能从物理介质中读取更多的字节(比如 1024 个字节),以提高性能。由于上述错误,额外的 1022 字节不会发送到解压缩例程。