Jaslabs: High performance Software

High Performance Software

compressing files in PHP

by Justin Silverton

Zlib compression has been built into php since version 3 and it can be used to compress the output of your php applications (which can significantly decrease the amount of bandwidth of a page), but what you can also do is compress any file accessible from your webserver.

The code

The following are two functions: compress and uncompress, which can compress and uncompress a specified file.

function uncompress($srcName, $dstName) {
$string = implode(”", gzfile($srcName));
$fp = fopen($dstName, “w”);
fwrite($fp, $string, strlen($string));
fclose($fp);
}

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(”test.php”,”test.gz”);
uncompress(”test.gz”,”test2.php”);

Source code can be downloaded here

Description of related zlib functions

gzclose — Close an open gz-file pointer
gzcompress — Compress 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

More information on zlib related functions can be found here

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • DZone
  • Slashdot
  • StumbleUpon
  • Technorati

8 Comments so far

  1. Cruella DeVille February 5th, 2007 1:13 pm

    Hey and thanks for a great function! But when I use it my filenames get *.doc.gz and if I remove the .doc in the input file the gz-file is okay, but the zipped file has no extension.

    Is there an easy way to solve this?

  2. Justin Silverton February 5th, 2007 8:15 pm

    how are you calling these functions?

    Have you tried:

    compress(”test.doc”,”test.gz”);

  3. Cruella DeVille February 6th, 2007 12:34 pm

    I call it like you say, only that I use variables. I manage to get til gz-filename right, but if I open the compressed whatever I get a file of filetype file, ie the originally extension is gone. I also found out that srcName must be a abs path, since my files are not located in the same directory, but guess that makes no different.
    function compress($srcName){
    $fileinfo = pathinfo($srcName);
    $ext = $fileinfo[’extension’];
    $dstName = PATH. basename($srcName, “.$ext”) . “.gz”;
    $fp = fopen($srcName, ‘r’);
    $data = fread ($fp, filesize($srcName));
    fclose($fp);
    $zp = gzopen($dstName, “w9″);
    gzwrite($zp, $data);
    gzclose($zp);
    }

    my function, slightly different from yours, one parameter, removes the extension of the sourceName and puts on gz instead of using filename as param… I’ve tried both ways, same results.

    Are your compressed files unalteret (regarding extension)?

  4. Keith May 1st, 2007 8:39 am

    Great piece of information. I have been looking for this gz compression tool to be called in PHP for quite some time and yours come in handy for my site, where I had to compress 1.2MB of sitemap file to conserve bandwidth.

    Cheers!!

  5. Rolf September 17th, 2007 2:58 pm

    Hello

    But… that don’t work with big files, someone knows how to uncompress a big file .gz?

    Regards

  6. katya November 7th, 2007 10:05 pm

    hello there.

    thanks for the info. but can i use compress and uncompress functions for directories?

    i need to compress temp directory and send the .zip output to website for download (using php).

    i tried to use zipfile class yesterday but the output is corrupted.

    please help.

    thanks!

  7. Soorya March 7th, 2008 6:14 am

    I executed this code but how this can be used for compresiing a file in client side

Leave a reply