Introduction
The Zip format was introduced in 1989. Creating a Zip file using PHP is very simple. PHP has a very useful class ZipArchive to create and manipulate Zip files. In this article I will show how to create a Zip file and how to use it's functions. In the Zip file creation program I will use open() to open or create a Zip file and addFile() to add a file to the archive. The close() function is used for safety to close the Zip file.
Before creating the sample Zip file, in the "C:\wamp\www" path, the files look like:
In the above image, no "newzip.zip" file exists yet.
Example of creating A Zip file in PHP
The following is sample code for creating a Zip file in PHP:
|
<?php $zip=newZipArchive(); $ow=1; $file="newzip.zip"; if($zip->open($file,$ow?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE) { $zip->addFile("test.txt"); $zip->close(); } |
When the preceding code is run, it will produce a Zip file named "newzip.zip".
PHP Zip File functions
The PHP Zip file functions provides many operations on Zip files. The following table lists the Zip functions.
| Function | Description |
| zip_open() | It open a Zip file archive. |
| zip_read() | It reads the next entry in a Zip file archive. |
| zip_close() | It closes the Zip file archive. |
| zip_entry_open() | It open a directory entry for reading. |
| zip_entry_read() | It read from an open directory entry. |
| zip_entry_close() | It closes a directory entry. |
| zip_entry_file_size() | It retrieve the actual file size of directory entry. |
| zip_entry_compressedsize() | It retrieve the compressed size of a directory. |
| zip_entry_compressionmethod() | It retrieve the compression method size of a directory. |
Here I explain each and every function separately.
PHP Zip open function
The PHP Zip open function is used to open a Zip archive for reading. This function returns an open Zip file resource on success, or false on failure.
Syntax
|
zip_open("filename"); |
Parameter in zip_open function:
| Parameter | Description |
| filename | It specifies the file name. |
Example
|
<?php
$Zip=zip_open("test.zip");
if(!$Zip)
{
echo "Unabletoopenzipfile";
} else {
echo "<b>Opend zip file</b>"; } |
Output
PHP Zip read function
The PHP zip_read function is used to read a file in an open Zip archive.
Syntax
|
zip_read("filename); |
Parameter in zip_read function:
| Parameter | Description |
| filename | It specifies the file name. |
Example
|
<?php
$Zip=zip_open("test.zip");
if(!$Zip)
{
echo "Unabletoopenzipfile";
}
else
{ zip_read($Zip);
} |
PHP Zip close function
The PHP zip_close function is used to close the Zip file archive.
Syntax
|
Zip_close("filename); |
Parameter in zip_close function:
| Parameter | Description |
| filename | It specifies the file name. |
Example
|
<?php
$Zip=zip_open("test.zip");
if(!zip_open("test.zip"))
{
echo "Fileisnotopenorunavailable";
}
else
{
Zip_close($Zip);
echo "Zipfileisclosed.";
} |
Output
PHP Zip entry open function
The PHP zip_entry_open function is used to open a Zip archive entry for reading.
Syntax
|
zip_entry_open($Zip,$zipEntry,mode) |
Parameter in zip_entry_open function:
| Parameter | Description |
| zip | It specifies a zip entry resource to read. |
| zipEntry | It specifies a zip entry resource to open. |
| mode | It specifies the type of access you require to the Zip archive. |
Example
|
<?php
$Zip=zip_open("test.zip");
if(!$Zip)
{
echo "Unabletoopenzipfile";
}
else
{
while($zipEntry=zip_read($Zip))
{
if(zip_entry_open($Zip,$zipEntry))
{
echo "Fileishasbeenopened";
}
}
}
?>
|





Nitin BhardwajPosted Apr 18, 2013, 6:15 AM
Nice Article Sharad