|
|
CXIII. Zlib Compression Functions
이 모듈은 Jean-loup Gailly 과 Mark Adler 의 사용하며 gzip(.gz)로 압축된 파일을 잘 읽고 쓸수 있다. 이 모듈은 zlib 1.0.9버젼 이상과 사용할 수 있다.
이 모듈은 gzip 압축된 파일과 연동하는 대부분의 filesystem 함수 버젼을 포함하고 있다. (and uncompressed files, too, but not
with sockets).
참고:
현재 CVS 4.0.4-dev 버젼은 .gz-files를 위한 fopen-wrapper를 소개하고 있다. 압축된 파일을
다루는 특별한 'zlib:'URL을 사용할 수 있기 위하여 일반적인 f*() 파일 제어 함수가 사용되며 fopen()함수 호출 전에 이미 알고 있는 파일명이나 경로가 'zlib:'와 함께 선행되어야 한다.
이 경우 fopencookie() 함수를 제공하는 C 런타임 라이브러리가 필요하다. 현재 이것을 제공하는 라이브러리는 GNU libc가 유일하다.
Small code example
임시 파일을 열어 테스트 문자를 기록하고, 그 내용을 두번 출력한다 .
예 1. Small Zlib Example <?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";
// open file for writing with maximum compression
$zp = gzopen($filename, "w9");
// write string to file
gzwrite($zp, $s);
// close file
gzclose($zp);
// open file for reading
$zp = gzopen($filename, "r");
// read 3 char
print gzread($zp, 3);
// output until end of the file and close it.
gzpassthru($zp);
print "\n";
// open file and print content (the 2nd time).
if (readgzfile($filename) != strlen($s)) {
echo "Error with zlib functions!";
}
unlink($filename);
print "</pre>\n</h1></body>\n</html>\n";
?> |
|
User Contributed Notes Zlib Compression Functions |
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 |
| |