Wednesday, October 12, 2011

Decompressing Archive Files Using the Zend Framework

Working with archives such as Zip, Tar, and Gz in PHP can be cumbersome at best and nightmarish at worst. The native Zip extension can be unpredictable and difficult to work with…but luckily, Zend to the rescue!
The Zend Framework has an extensive library of filter components that can modify and format your data in a dizzying array of different ways, including working with archives. The component we are interested is Zend_Filter_Decompress.
To decompress an archive file, we simply create an instance of the Zend_Filter_Decompress class, tell it which type of archive we are working with and where to save the decompressed files, then call the object’s filter() method, passing in the path to the archive file.


//Create filter object

$filter = new Zend_Filter_Decompress(array(

'adapter' => 'Zip', //Or 'Tar', or 'Gz'

'options' => array(

'target' => '/path/to/output/directory'

)

));


$filter->filter('/path/to/archive/file.zip');

That’s all there is to it. If you look at the contents of your output directory, you’ll see the contents of your archive file. Compressing files into an archive is slightly more involved (but still simplified by the Zend Framework!) and will be covered in a future article.

No comments:

Post a Comment