Compression and Decompression for ZF

With Zend Framework Release 1.10 there will be a way to handle compression and decompression.

The new components Zend_Filter_Compress and Zend_Filter_Decompress provide a unified API for several compression formats.

Actually the formats BZ2, GZ, LZF, RAR, TAR and ZIP are implemented:

You can not only work with Strings, but also with Files and also with complete Directories.
Each format is provided as standalone Adapter. This way we can easily add support for a new compression format by adding a new Adapter.

So let’s go a little bit into details…
To compress a string with BZ2 we would code the following lines:

$filter = new Zend_Filter_Compress('bz2');
$input  = "Uncompressed content";
$output = $filter->filter($input);

$output will have stored the compressed string.
To reverse the output and get the uncompressed string simply do the following:

$filter = new Zend_Filter_Decompress('bz2');
$result = $filter->filter($output);

As you can see compression and decompression is really very simple.

Now when you want to compress and decompress files, it is as simple as compressing strings.
Simply give the filename instead of a string and tell the adapter where to store the archive.

$filter = new Zend_Filter_Compress(array(
    'adapter' => 'bz2'
    'options' => array(
        'archive' => 'myarchive.bz2'
$result = $filter->filter('\\path\to\myfile.ext');

The file myfile.ext will be compressed and stored into myarchive.bz2.
You can even compress complete directories. But be aware, that when you do this on the base directory of your server, your server will be stressed for a very long time.

As you can attach this filters to any component which accepts filters, you could very easily compress any given input to store it anywhere else.

Let’s expect you have a textarea element where your costumer provides a book review. To save space you could easily attach this filter to the element and store the compressed string into the database. When you want to display the content to your user again simply decompress it before rendering.

$element = new Zend_Form_Element_TextArea('myarea');
$element->addFilter('Compress', array('zip'));

There are many places where compression or decompression can be useful. In all this cases you can now use Zend_Filter_Compress.

When you find this component useful feel free to use it within your own application.
Have fun with Zend Framework

Thomas Weidner
I18N Team Leader, Zend Framework

Zend Framework Advisory Board Member
Zend Certified Engineer for Zend Framework

Back to top
  1. chrisweb

    Tuesday, September 22, 2009 - 22:06:41

    Nice! Thats a really cool filter.

    If we want to put the zipped text in the database, we have to use the blob type for the db field?

    If we use the compression filter to create a zip as you explained, does the filter save the zip somewhere on the server harddisc before you submit the form and then the form sends you the zpped text as a file in $_FILES? Do we have to read that zip if we want to insert it into the database?

    For example something like this:

    $filePath = $form->fileUpload->getFileName();
    $fp = fopen($filePath, ‘r’);
    $data = fread($fp, filesize($filePath));


    $data = file_get_contents($filePath);

    $db = new Module_Model_DbTable_Data();


  2. thomasw

    Tuesday, September 22, 2009 - 23:15:55

    Why would you have to use blob ? Text is text… if it’s readable or not does not matter for the database.

    Why should the filter send a file to the user ? It’s a filter… what ever is done befor or after the filter depends on your implementation.

    I have never heared that $_FILES is used for downloads to the user… as the PHP manual states it is only filled for uploads from the user to the server.

    Your questions are irritating…
    This component is a filter, nothing more, nothing less. Filters are attached to components which allow to use filters. This has nothing to do with database nor with uploads.

    Of course you can attach this filter also to a file element.

  3. Ananda

    Wednesday, October 14, 2009 - 07:18:24

    Where can i download , the Zend_Filter_Compress for my project work

  4. thomasw

    Wednesday, October 14, 2009 - 12:07:06

    From the same place as the framework itself…

  5. Barbi

    Thursday, April 14, 2011 - 19:51:22

    Glad I’ve flinlay found something I agree with!

  6. frank

    Friday, November 11, 2011 - 15:36:24

    I have been searching the web for days and noticed that nobody cared to post anything about how an update form should manage an empty file field.

    I have been trying to understand why when I update a form the old file in the database is erased even though the file field is empty!!

    Is there any chance you could light us up?

  7. thomasw

    Sunday, November 13, 2011 - 00:40:13

    This has nothing to do with compression or decompression filters.

    I would suggest that you ask within the mailing list or at irc. I am also available at these places from time to time (not 24h :-) )

  8. Anton

    Sunday, March 4, 2012 - 02:59:40

    Hi, Is there any possibility to set up a callback for Zend_Filter_Compress ->filter() call? I am compressing files but need to know when the compression is finished so I can delete the original files?

  9. ethylotests

    Monday, April 16, 2012 - 03:58:03

    yeah, i think you are right !
    ethylotest are important and alcootest too !

  10. Ernie

    Wednesday, May 23, 2012 - 13:02:20

    Great post, never heard about it, check my website if you wand something fun.
    Visiting is a great opportunity to have fun :)