



(12 votes)/* 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); }