Create Zip File in PHP

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:

create-zip1-in-php.jpg

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".

create-zip2-in-php.jpg

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

zip-open-in-php.jpg

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

zip-close-in-php.jpg 


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";

 

}

 

}

 

}

 

?>

 


Output

zip-entry-open-in-php.jpg
 

PHP zip_entry_read function

The zip_entry_read function is used to read from an open directory entry.

Syntax
 

zip_entry_read(zipEntry,length)

Parameter in zip_entry_read function:

Parameter Description
length It is optional parameter.
zipEntry It specifies a zip entry resource to readwhich is previously opend by zip_entry_open function.

Example

<?php

$zip=zip_open("test.zip");

if(!$zip)

 

{

 

echo "Unabletoopenzipfile";

 

}

else

  {

  while ($zip_entry = zip_read($zip))

    {

    echo "<p>";

    echo "Name: " . zip_entry_name($zip_entry) . "<br/>";

 

    if (zip_entry_open($zip, $zip_entry))

      {

      echo "File Data:<br/>";

      $contents = zip_entry_read($zip_entry);

      echo "$contents<br/>";

      }

    echo "</p>";

  }

 

zip_close($zip);

}
?>


Output

zip-entry-read-in-php.jpg

PHP zip entry close function

The PHP zip_entry_close function is used to closes a Zip archive opened by the zip_entry_open function.

Syntax
 

zip_entry_close(zipEntry)

Parameter in zip_entry_close function:

Parameter Description
zipEntry It specifies a directory previously opend by zip_entry_open function.

Example

<?php

 

$zip=zip_open("test.zip");

 

if(!$zip)

 

{

 

echo "Fileisnotopenorunavailable";

 

}

 

else

 

{

 

while ($zipEntry = zip_read($zip))

 

{

 

echo "Name:" . zip_entry_name($zipEntry)."<br/>" ;

 

if (zip_entry_open($zip, $zipEntry))

 

{

 

zip_entry_close($zipEntry);

 

echo "File has been closed.";

 

}

 

}

 

zip_close($zip);

 

}
?>


Output

zip-entry-close-in-php.jpg
 

PHP zip_entry_compressedsize function

The PHP zip_entry_compressedsize function is used to return the compressed file size of a Zip archive entry.

Syntax
 

zip_entry_compressedsize(zipEntry)

Parameter in zip_entry_compressedsize function:

Parameter Description
zipEntry It specifies a zip entry resource to read.

Example

<?php

$zip=zip_open("test.zip");

if(!$zip)

{

 

echo "Unabletoopenzipfile";

 

}

else

  {

  while ($zip_entry = zip_read($zip))

    {

    echo "Name" .zip_entry_name($zip_entry)."</br>";

    echo "Compressed size is:".zip_entry_compressedsize($zip_entry);

 

  }

 

zip_close($zip);

}
?>


Output

zip-entry-compressed-size-in-php.jpg
 

PHP zip_entry_compressionmethod()

The PHP zip_entry_compressionmethod function is used to return the compression method of a Zip archive entry.

Syntax
 

zip_entry_compressionmethod(zipEntry)

Parameter in zip_entry_compressionmethod function:

Parameter Description
zipEntry It specifies a zip entry resource to read.

Example

<?php

 

$Zip=zip_open("test.zip");

 

if(!$Zip)

 

{

 

echo "Fileisnotopenorunavailable";

 

}

 

else

 

{

 

while($zipEntry=zip_read($Zip))

 

{

 

echo "Name:". zip_entry_name($zipEntry)."<br/>";

 

echo "Compression method: ". zip_entry_compressionmethod($zipEntry);

 

}

 

zip_close($Zip);

 

}
?>


Output

zip-entry-compressionmethod-in-php.jpg