|
|
| PHP Essentials (ISBN: 076152729X) |
 |
List Price: $39.99
Our Price: $31.99
Used Price: $21.95
Release Date: 01 March, 2000
Manufacturer: Premier Press (Paperback)
Sales Rank: 14,771
Author: Julie Meloni
|
More Info
|
|
|
Compress Data Using ZLib
|
2003-06-22 20:08:01
|
| |
php
|
|
|
|
|
Category: source:php:math
|
|
Description: Use the zlib functions in php to compress a file.
|
|
Platform: all
|
|
Author: detour
|
|
Viewed: 5241
|
|
Rating: 2.8/5 (12 votes)
|
|
|
| If you have any questions about
this piece of code or still need help, try posting your question on the forum. |
<?php
/* gzip.php by detour@metalshell.com
*
* Use the zlib functions in php to compress a file.
*
* http://www.metalshell.com/
*
*/
/* Clearly this string isn't worth compressing as it will get
bigger with the headers, but you get the idea. */
$str = "Compressed string";
$gzfile = "/tmp/file.gz";
// Compress our string.
write_gzip_file($str, $gzfile);
// Read the compressed file and display it.
print read_gzip_file($gzfile);
function read_gzip_file($file) {
// Open gzip for reading.
$fpgz = gzopen($file, "r");
// Read the data.
$retval = gzread($fpgz, filesize($file));
// Close the file.
gzclose($fpgz);
return $retval;
}
function write_gzip_file($data, $file) {
// Open the gzip file stream. The 9 is the compression level.
$fpgz = gzopen($file, "w9");
// Compress as the string is written.
gzwrite($fpgz, $data);
// Close the stream.
gzclose($fpgz);
}
?>
|
|
|