ZIP Files Management in PHP 5.2.0

Mar 13, 2008 16:47 GMT  ·  By

The ZIP archives are very popular and extensively used for data compression. For commonly used operating systems like Windows, Linux and MacOS, there are many utilities that help you to manage ZIP archives, like 7-Zip for example. When you need to manage ZIP files with the help of PHP web applications running on server side, multiple alternatives exist.

The ZIP archives manipulation can be performed by using various classes or the PHP ZIP extension, which enables you to handle the creation of archives having ZIP compression type. On Linux operating systems, the PHP must be compiled with support for ZIP archives and on Windows operating systems the extension php_zip.dll must be enabled by editing the corresponding line in the php.ini file.

In order to simplify the task of PHP configuration to enable support for ZIP archives management on Windows operating system, WAMP application allows you to install the PHP ZIP extension in a visual manner by checking the corresponding entry in the PHP extensions menu.

For other LAMP distributions for Windows, having PHP version 5.2.0 or higher, the PHP ZIP extension must be enabled by uncommenting the line #extension=php_zip.dll (delete # character) in the php.ini configuration file. Then the php.ini file must be saved and the Apache server restarted in order to reflect the new changes in the PHP configuration.

By using certain PHP scripts, you will be able to extract or add files to a newly created or existing ZIP archive, list archive contents and more. For example, by using the next PHP, script various files can be added to a new ZIP archive:

[CODE=0][CODE=1]

If you want to test this script, copy and paste it into a text file that must be saved with .php extension. It will create a ZIP archive called test.zip containing two files ex1.doc and ex2.doc, if they are in the same directory with test.php script. The new ZIP archive object is created in the third line of the script.

The lines 6 and 7 can be multiplied corresponding to the number of files you want to be in the ZIP archive. You can add your own documents or files in an archive by specifying their complete name in the argument of addFile directive. The archive name can also be changed in line 2. Supplementary operations can also be performed over the ZIP archive by using various others functions. The complete description of functions related to ZIP files management in PHP can be found here.