如何使用 gzip 标头和不压缩标头输出数据


How output data with gzip header and not deflate header

>我正在测试html文件的压缩。

我有 2 个 HTML 文件:

  1. 未压缩的HTML文件(内容会更改)
  2. 压缩的HTML文件.gz(内容不会更改)

使用 PHP,我正在尝试输出压缩文件数据,这里开始了。

使用已压缩的 HTML 文件进行测试。

//header gzip
$data = getfile($name); // custom function packed with fopen fread
header(Content-Encoding: gzip); // header works perfect
echo $data; // output OK
//header deflate
$data = getfile($name); // custom function packed with fopen fread
header(Content-Encoding: deflate); // file was gzip compressed so error is normal
echo $data; // fireFox : Content Encoding Error

使用未压缩的 HTML 文件进行测试

//header gzip using gzcompress();
$data = gzcompress(getfile($name), 9); 
header(Content-Encoding: gzip); // somehow header is bad
echo $data; // fireFox : Content Encoding Error , but IE 9 output OK

但在这里我们得到了魔力

//header deflate using gzcompress();
$data = gzcompress(getfile($name), 9); 
header(Content-Encoding: deflate); // header works perfect
echo $data; // Firefox output OK, but IE output ERROR

如何解决这个疯狂的事情并将所有数据作为 gzip 发送,带有 gzip 标头而不是 defalte? 也许有人知道出了什么问题?

谢谢

> HTTP 规范 (RFC2616) 说:

噗��

    An encoding format produced by the file compression program
    "gzip" (GNU zip) as described in RFC 1952 [25].

压缩

    The encoding format produced by the common UNIX file compression
    program "compress".

紧缩

    The "zlib" format defined in RFC 1950 [31] in combination with
    the "deflate" compression mechanism described in RFC 1951 [29].

PHP文档说:

GZ压缩

   For details on the ZLIB compression algorithm see the document
   "ZLIB Compressed Data Format Specification version 3.3"
   (RFC 1950).

GZ放气

   For details on the DEFLATE compression algorithm see the document
   "DEFLATE Compressed Data Format Specification version 1.3"
   (RFC 1951).

格森科德

   For more information on the GZIP file format, see the document:
   GZIP file format specification version 4.3 (RFC 1952).

由此可以得出结论,gzencode()必须与gzip一起使用,而gzcompress()(使用DEFLATE编码)必须与deflate一起使用。

第一个组合对我有用。我没有尝试过第二个;不知道为什么它不适用于IE。URL 可能有助于排查该问题。