PHP  
downloads | documentation | faq | getting help | mailing lists | | php.net sites | links | my php.net 
search for in the  
<zip_readgzclose>
view the version of this page
Last updated: Sat, 19 Apr 2003

CXIII. Zlib Komprimierungsfunktionen

Diese Extension nutzt die Funktionen der von Jean-loup Gailly und Mark Adler um transparent gzip (.gz) - komprimierte Dateien lesen und schreiben zu k�nnen. Es muss die zlib >= 1.0.9 verwendet werden, um die Funktionen nutzen zu k�nnen.

Diese Extension beinhaltet Versionen der meisten Dateisystem-Funktionen, die mit gzip-komprimierten (und nicht komprimierten) Datein, jedoch nicht mit Sockets funktionieren.

Anmerkung: Mit Version 4.0.4 wurde ein fopen-wrapper f�r .gz-Dateien eingef�hrt, so das eine spezielle 'zlib:'-URL verwendet werden kann, um komprimierte Datei transparent �ber die f*()-Dateifunktionen genutzt werden k�nnen.

In Version 4.3.0 wurde diese spezielle Prefix in zlib:// ge�ndert, um Konflikte mit Dateinamen zu vermeiden die ':' enthalten.

Dieses Feature erfordert eine C-Laufzeitbibliothek, die die fopencookie()-Funktion bereitstellt. Nach aktuellem Stand ist die GNU libc die einzige Bibliothek, die dieses Feature bereitstellt.

Code-Beispiel

�ffnet eine tempor�re Datei und schreibt einen Test-String in die Datei, dann wird der Inhalt dieser Datei zweimal ausgegeben.

Beispiel 1. Zlib Beispiel

<?php

$filename = tempnam ('/tmp', 'zlibtest').'.gz';
print "<html>\n<head></head>\n<body>\n<pre>\n";
$s = "Only a test, test, test, test, test, test, test, test!\n";

// �ffne eine Datei zum Schreibzugriff mit der maximalen Kompressionsstufe
$zp = gzopen($filename, "w9");

// Schreibe den String in die Datei
gzwrite($zp, $s);

// Schlie�e die Datei
gzclose($zp);

// �ffne die Datei zum Lesezugriff
$zp = gzopen($filename, "r");

// Lese 3 Zeichen
print gzread($zp, 3);

// Gebe den Inhalt aus, bis die Datei vollst�ndig ausgegeben ist.
gzpassthru($zp);

print "\n";

// �ffne die Datei und gebe den Inhalt zum zweiten Mal aus
if (readgzfile($filename) != strlen($s)) {
        echo "Fehler in den Zlib-Funktionen!";
}
unlink($filename);
print "</pre>\n</h1></body>\n</html>\n";

?>
Inhaltsverzeichnis
gzclose -- Close an open gz-file pointer
gzcompress -- Compress a string
gzdeflate -- Deflate a string
gzencode -- Create a gzip compressed string
gzeof -- Test for end-of-file on a gz-file pointer
gzfile -- Read entire gz-file into an array
gzgetc -- Get character from gz-file pointer
gzgets -- Get line from file pointer
gzgetss --  Get line from gz-file pointer and strip HTML tags
gzinflate -- Inflate a deflated string
gzopen -- Open gz-file
gzpassthru --  Output all remaining data on a gz-file pointer
gzputs -- Alias for gzwrite()
gzread -- Binary-safe gz-file read
gzrewind -- Rewind the position of a gz-file pointer
gzseek -- Seek on a gz-file pointer
gztell -- Tell gz-file pointer read/write position
gzuncompress -- Uncompress a deflated string
gzwrite -- Binary-safe gz-file write
readgzfile -- Output a gz-file


User Contributed Notes
Zlib Komprimierungsfunktionen
add a note
monte at ispi dot net
19-Apr-2001 01:02

An alternate way to handle gzip compression is to let the mod_gzip module of apache handle it. This seems to contradict the tutorial on phpbuilder.com saying that it won't compress php (or any dynamic) output, but mod_gzip as of version 1.3.17.1a works well for me.

Here is an example of an httpd.conf setup:

<IfModule mod_gzip.c>
mod_gzip_on                 Yes
mod_gzip_dechunk          Yes
mod_gzip_minimum_file_size 300
mod_gzip_maximum_file_size  0
mod_gzip_maximum_inmem_size 100000
mod_gzip_keep_workfiles     No
mod_gzip_temp_dir           /tmp
mod_gzip_item_include       file \.html$
mod_gzip_item_include       file \.jsp$
mod_gzip_item_include       file \.php$
mod_gzip_item_include       file \.pl$
mod_gzip_item_include       mime ^text/.*
mod_gzip_item_include       mime ^application/x-httpd-php
mod_gzip_item_include       mime ^httpd/unix-directory$
mod_gzip_item_include       handler ^perl-script$
mod_gzip_item_include       handler ^server-status$
mod_gzip_item_include       handler ^server-info$
mod_gzip_item_exclude       mime ^image/.*
</IfModule>

This will automatically compress all output of your files with the .php extention or the x-httpd-php mime type. Be sure to have dechunk set to Yes.

STEVE vortexx at freemail dot hu
20-Jan-2003 12:33

The manual here states that the option zlib.output_compression can be changed at runtime using ini_set:

Name Default Changeable
zlib.output_compression "Off" PHP_INI_ALL (!)
zlib.output_compression_level "-1" PHP_INI_ALL
zlib.output_handler "" PHP_INI_ALL

However, using PHP 4.3.0 on Win2k this is not so. Trying to set zlib.output_compression to "On" from within a PHP script returns empty (meaning ini_set was unsuccessful)!

mlevy at rgj dot com
12-Feb-2003 09:06

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.
wallacebw at yahoo dot com
09-Mar-2003 03:10

I have found that using:

zlib.output_compression = on (or On)

instead of using a chunk size (i.e. 4KB)

is problematic when used in conjunction with output buffering i.e. 'ob_start()' and form data.  PHP intermittently fails to return any data to client after processing form.  

Using a chunk size appears to correct this problem.

Brian Wallace

phpuser at dpiworld.com
21-Mar-2003 02:32

With zlib.output_compression turned on in the php.ini file, I discovered that the output of my script which returns jpeg data was being compressed, which for some reason causes IE 6.0 to take much longer to display the image.  Turning off compression cured the problem.  So I tried calling ini_set("zlib.output_compression", 0 ) (also tried "Off")
as the first statement in my script.  It does not appear to work. Then I noticed that the documentation is inconsistant, here it says zlib.output_compression can be changed anywhere (PHP_INI_ALL), yet in the docs for ini_set() it says that it can only be changed in PHP_INI_SYSTEM or PHP_INI_PERDIR. If I understand correctly that means ini_set() can't change it in a script.  Apparently the latter is true.

devcontact at tech-island dot com
24-Mar-2003 02:47

The method of first reading the source file and then passing its content to the gzip function instead of simply the source and destination filename was a bit confusing for me.

So I have written a simple funtion you can use to compress files in the gzip format (gzip is readable by winzip like .zip files)

function compress($srcName, $dstName)
{
$fp = fopen($srcName, "r");
 $data = fread ($fp, filesize($srcName));
 fclose($fp);

 $zp = gzopen($dstName, "w9");
 gzwrite($zp, $data);
gzclose($zp);
}

// Compress a file
compress("/web/myfile.dat", "/web/myfile.gz");

add a note

<zip_readgzclose>
 Last updated: Sat, 19 Apr 2003
show source | credits | mirror sites 
Copyright © 2001-2003 The PHP Group
All rights reserved.
This mirror generously provided by: /
Last updated: Thu May 15 01:08:38 2003 CEST