PHPのお勉強!

PHP TOP

Zlib 関数

目次

  • deflate_add — インクリメンタルにデータを圧縮する
  • deflate_init — インクリメンタルな圧縮コンテキストを初期化する
  • gzclose — 開かれたgzファイルへのポインタを閉じる
  • gzcompress — 文字列を圧縮する
  • gzdecode — gzip 圧縮された文字列をデコードする
  • gzdeflate — 文字列を deflate 圧縮する
  • gzencode — gzip 圧縮された文字列を作成する
  • gzeof — gz ファイルポインタが EOF かどうか調べる
  • gzfile — gzファイル全体を配列に読み込む
  • gzgetc — gz ファイルへのポインタから文字を得る
  • gzgets — ファイルポインタから 1 行を得る
  • gzgetss — gzファイルへのポインタから1行を得て、HTMLタグを取り除く
  • gzinflate — deflate圧縮された文字列を解凍する
  • gzopen — gz ファイルを開く
  • gzpassthru — gzファイルへのポインタから残りのデータ全部を出力する
  • gzputs — gzwrite のエイリアス
  • gzread — バイナリ対応のgzファイル読み込み
  • gzrewind — gz ファイルポインタの示す位置を元に戻す
  • gzseek — gz ファイルポインタの位置を移動する
  • gztell — gz ファイルポインタの読み込み/書き込み位置を返します
  • gzuncompress — 圧縮された文字列を解凍する
  • gzwrite — バイナリセーフな gz ファイル書き込み
  • inflate_add — インクリメンタルにエンコードされたデータを解凍する
  • inflate_get_read_len — これまでに読み取ったバイト数を得る
  • inflate_get_status — 解凍の状態を取得する
  • inflate_init — インクリメンタルな解凍コンテキストを初期化する
  • ob_gzhandler — 出力バッファを gzip 圧縮するための ob_start コールバック関数
  • readgzfile — gz ファイルを出力する
  • zlib_decode — raw/gzip/zlib で符号化されたデータを復元する
  • zlib_encode — 指定した符号化方式でデータを圧縮する
  • zlib_get_coding_type — 出力圧縮に使用されたコーディングの種類を返す
add a note

User Contributed Notes 19 notes

up
8
anzenews at volja dot net
18 years ago
php at seven dot net dot nz:
Have you been using register_shutdown_function() that outputs something? If yes, some of the output was not encoded and IE6 is less than forgiving about that. In my case it was reloading the page and sometimes showing a blank page. So IE6 does get invalid stream, bug is not bogus, only description is not good enough.

jpleveille at webgraphe dot com:
In PHP 4.4.2 register_shutdown_function() DOES output whatever you wish, but it is not encoded with the rest of the document. Which is even worse as you get a bug that is difficult / impossible to track down.
up
5
zigazou at free dot fr
20 years ago
If you use "zlib.output_compression = On" in your php.ini file, and activates output buffering (ob_start), don't output this header :
header('Content-Length: '.ob_get_length());

This is because ob_get_length() will return the uncompressed size while zlib will compress the output. Thus your browser will get confused waiting for extra data that will never come.
up
4
gem at rellim dot com
20 years ago
Run, do not walk, run, to add this to your Apache config file:

php_flag zlib.output_compression On
php_value zlib.output_compression_level 5

I just tried this and achieved 10x and 15x speed inprovement on
some mature php pages. Pages I have been seating over to make 5% gains on. I use microtime() on critical pages to help me track page speed and that confirms the speed improvement. The php page takes a timestamp at the beginning and end, then logs the page duration. So any IP transmission effects are not included. There is
a clear subjective difference to the user.

The test system was PHP 4.3.6, Apache 2.0.49 over Linux 2.4.
As always YMMV.
up
4
mlevy at rgj dot com
21 years ago
If you turn zlib.output_compression_level on, be advised that you shouldn't try to flush() the output in your scripts. PHP will add the gzip header but send the output uncompressed, which plays havoc with Mozilla. IE seems to handle it, though.
up
6
Brian
17 years ago
If you have zlib.output_compression set to on when your script starts (in php.ini or via apache directive), then you disable it at runtime using ini_set() before producing output, your output will still be buffered! Even if you call ob_implicit_flush().

If you want unbuffered output, you must disable zlib.output_compression before your script starts (as well as mod_gzip of course, if you have both installed).

This behaviour experienced on Server: Apache/1.3.33 (Debian GNU/Linux) mod_gzip/1.3.26.1a PHP/4.3.10-18
up
5
admin_at_commandline_dot_ch
20 years ago
My gzip function.

This function read, compress and writhe only small chunks at one time, this way you can compress big files without memory problems...

<?php
function gzip($src, $level = 5, $dst = false){
if(
$dst == false){
$dst = $src.".gz";
}
if(
file_exists($src)){
$filesize = filesize($src);
$src_handle = fopen($src, "r");
if(!
file_exists($dst)){
$dst_handle = gzopen($dst, "w$level");
while(!
feof($src_handle)){
$chunk = fread($src_handle, 2048);
gzwrite($dst_handle, $chunk);
}
fclose($src_handle);
gzclose($dst_handle);
return
true;
} else {
error_log("$dst already exists");
}
} else {
error_log("$src doesn't exist");
}
return
false;
}
?>
up
5
Robin Skoglund
16 years ago
ini_set('zlib.output_compression', 'On') doesn't work

It says that zlib.output_compression is PHP_INI_ALL changable, but in my case at least, this is not true. When using ini_set('zlib.output_compression', 'On') runtime, phpinfo() states that zlib.output_compression is enabled, yet there is no compression.

Since I don't want to enable zlib.ouput_compression for all sites in php.ini, my solution was to enable it in apache conf (.htaccess should work as well) with the line:
php_flag zlib.output_compression on

Just spent hours figuring this out, so I hope it helps other people.

Btw, I'm using Apache/2.2.4 (Ubuntu) PHP/5.2.3-1ubuntu6.3.
up
3
Bob
19 years ago
Contrary to what the documentation says, I've been unable to get zlib.output_compression to work via ini_set() (Even though I put it at the very beginning of the file before any output was sent) as of php 4.3.11. While it does get set to true, it will not actually do anything. Which means if you don't set this via php.ini or Apache configuration it's a no-go. I have to use ob_start("ob_gzhandler"); instead.
up
3
djmaze(AT)dragonflycms(.)org
19 years ago
If you need to compress data and send it as "Content-disposition: attachment" and on-the-fly to the client due to the size for example (40Mb) here's a dirty trick using ob_gzhandler()

Keep in mind that $str is the content to output.

When you start the output call

<?php
echo ob_gzhandler($str, PHP_OUTPUT_HANDLER_START);
?>

Then to output any further content

<?php
echo ob_gzhandler($str, PHP_OUTPUT_HANDLER_CONT);
?>

And to close the output

<?php
echo ob_gzhandler('', PHP_OUTPUT_HANDLER_END);
exit;
?>

Only tested on Apache 1.3.33 with PHP 5.0.4
up
2
Arne dot Heizmann at csr dot com
18 years ago
PHP Version 5.1.4 here. What Bob said is correct even in this version (newest at the time of writing). You can't enable zlib.output_compression via ini_set(). You have to use php.ini.
up
1
spam at wildpeaks dot com
20 years ago
For decompressing, i modified a function posted earlier (that way $string doesn't have a big size that may be beyond the memory limit if the gzipped file is big) :

function file_ungzip($fromFile, $toFile) {
$zp = @gzopen($fromFile, "r");
$fp = @fopen($toFile, "w");
while(!@gzeof($zp)) {$string = @gzread($zp, 4096); @fwrite($fp, $string, strlen($string));}
@gzclose($zp);
@fclose($fp);
}