ѹ��������

��Ȼ ѹ����װЭ���ṩ���ڱ����ļ�ϵͳ�� ���� gzip �� bz2 �����ļ��ķ����������������������������ṩͨ��ѹ������˼��Ҳ��������Խ�һ����ѹ������ת����һ��ѹ�������Դˣ�ѹ���������������κ�ʱ��Ӧ�����κ�����Դ��

Note: ѹ�������� �����������й����� gzip��ͷ��β��Ϣ��ֻ��ѹ���ͽ�ѹ�������е���Ч�غɲ��֡�

zlib.deflate��ѹ������ zlib.inflate����ѹ��ʵ���˶����� » RFC 1951��ѹ���㷨�� deflate���������Խ�����һ���������鴫�ݵ�������������� level������ѹ��ǿ�ȣ�1-9�������ָ���ͨ���������С���غɣ���Ҫ���ĸ���Ĵ���ʱ�䡣������������ѹ���ȼ���0����ȫ��ѹ������ -1��zlib �ڲ�Ĭ��ֵ��Ŀǰ�� 6���� window��ѹ�����ݴ��ڴ�С���Զ��Ĵη���ʾ�����ߵ�ֵ���� 15 —— 32768 �ֽڣ��������õ�ѹ��Ч�������ĸ����ڴ棬�͵�ֵ���͵� 9 —— 512 �ֽڣ����������ϲ��ѹ��Ч�����ڴ����ĵ͡�ĿǰĬ�ϵ� window��С�� 15�� memory����ָʾҪ������ٹ����ڴ档�Ϸ�����ֵ��Χ�Ǵ� 1����С���䣩�� 9�������䣩���ڴ�����Ӱ���ٶȣ�����Ӱ�����ɵ��غɵĴ�С��

Note: ��Ϊ��õIJ�����ѹ���ȼ���Ҳ�����ṩһ������ֵ��Ϊ�˲��������������飩��

zlib.* ѹ���������� PHP �汾 5.1.0����ã��ڼ��� zlib��ǰ���¡�Ҳ����ͨ����װ���� » PECL�� » zlib_filter����Ϊһ�������� 5.0.x����ʹ�á��˹������� PHP 4 �� ��������

Example #1 zlib.deflate�� zlib.inflate

<?php
$params 
= array('level' => 6, 'window' => 15, 'memory' => 9);

$original_text = "This is a test.\nThis is only a test.\nThis is not an important string.\n";
echo 
"The original text is " . strlen($original_text) . " characters long.\n";

$fp = fopen('test.deflated', 'w');
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, $params);
fwrite($fp, $original_text);
fclose($fp);

echo 
"The compressed file is " . filesize('test.deflated') . " bytes long.\n";
echo 
"The original text was:\n";
/* Use readfile and zlib.inflate to decompress on the fly */
readfile('php://filter/zlib.inflate/resource=test.deflated');

/* Generates output:

The original text is 70 characters long.
The compressed file is 56 bytes long.
The original text was:
This is a test.
This is only a test.
This is not an important string.

 */
?>

Example #2 zlib.deflate�򵥲����÷�

<?php
$original_text 
= "This is a test.\nThis is only a test.\nThis is not an important string.\n";
echo 
"The original text is " . strlen($original_text) . " characters long.\n";

$fp = fopen('test.deflated', 'w');
/* Here "6" indicates compression level 6 */
stream_filter_append($fp, 'zlib.deflate', STREAM_FILTER_WRITE, 6);
fwrite($fp, $original_text);
fclose($fp);

echo 
"The compressed file is " . filesize('test.deflated') . " bytes long.\n";

/* Generates output:

The original text is 70 characters long.
The compressed file is 56 bytes long.

 */
?>

bzip2.compress�� bzip2.decompress�����ķ�ʽ�����潲�� zlib ��������ͬ�� bzip2.compress������������һ���������������������������� blocks�Ǵ� 1 �� 9 ������ֵ��ָ��������ٸ� 100K �ֽڵ��ڴ����Ϊ�������� work�� 0 �� 250 ������ֵ��ָ�����˻ص�һ����һЩ�������ɿ����㷨֮ǰ�����ٴγ���ѹ���㷨�ij��ԡ������˲�����Ӱ�쵽�ٶȣ�ѹ��������ڴ�ʹ�ö����ܴ����õ�Ӱ�졣���˲�����Ϊ 0 ָʾ bzip ��ʹ���ڲ�Ĭ���㷨�� bzip2.decompress������������һ����������������ͨ�IJ���ֵ���ݣ�������һ�����������е� small��Ԫ���ݡ��� small��Ϊ &true; ֵʱ��ָʾ bzip ������С���ڴ�ռ����ִ�н�ѹ�����������ٶȻ���һЩ��

bzip2.* ѹ���������� PHP �汾 5.1.0����ã��ڼ��� bz2֧�ֵ�ǰ���¡�Ҳ����ͨ����װ���� » PECL�� » bz2_filter����Ϊһ�������� 5.0.x����ʹ�á��˹������� PHP 4 �� ��������

Example #3 bzip2.compress�� bzip2.decompress

<?php
$param 
= array('blocks' => 9, 'work' => 0);

echo 
"The original file is " . filesize('LICENSE') . " bytes long.\n";

$fp = fopen('LICENSE.compressed', 'w');
stream_filter_append($fp, 'bzip2.compress', STREAM_FILTER_WRITE, $param);
fwrite($fp, file_get_contents('LICENSE'));
fclose($fp);

echo 
"The compressed file is " . filesize('LICENSE.compressed') . " bytes long.\n";

/* Generates output:

The original text is 3288 characters long.
The compressed file is 1488 bytes long.

 */
?>