Working With Zip Files In PHP

Introduction

 
Zip is a very common and the most popular data compression format. Most of the files are sent and received in zip format. It is a very useful format for combining and compressing multiple files together. In PHP it is very easy to work with zip files. PHP provides functions for all the common operations on zip files like creating a zip archive, adding files to the archive, viewing the contents of the archive, and extracting its contents. This article is a simple demonstration of the zipping capabilities in PHP.
 

Using the ZipArchive class

 
In this article, we will explore the common operations on zip files supported in PHP using the ZipArchive class. The ZipArchive instance represents a file compressed in the zip format.
 
Following are some of the zip operations supported by PHP,
 
Viewing the names of entries in a zip file
 
To view the names of entries in a zip file, we need to first create an object of the ZipArchive class and invoke the open() function passing the zip file name as a parameter. The numFiles property can be used to know the number of files in the archive. The getNameIndex() method returns the name of an entry in the zip file using its index
  1. echo "Contents of $filename<br/>";  
  2. $zip = new ZipArchive;  
  3. if ($zip - > open($filename)) {  
  4.     echo "<ol>";  
  5.     for ($i = 0; $i < $zip - > numFiles; $i++) {  
  6.         echo "<li>".$zip - > getNameIndex($i).  
  7.         "</li>";  
  8.     }  
  9.     echo "</ol>";  
  10.     $zip - > close();  
  11. }   
 
 
Viewing the contents of files within a zip file
 
To view the contents files within a zip file, we need to first create an object of the ZipArchive class and invoke the open() function passing the zip file name as a parameter. The numFiles property can be used to know the number of files in the archive. The getNameIndex() method returns the name of an entry in the zip file using its index. The getFromIndex() method returns the file contents using its index. The following sample code can be used to view all the file names and their contents in a zip archive by iterating over all files.
  1. $zip = new ZipArchive;  
  2. if ($zip - > open($filename)) {  
  3.     echo "<ol>";  
  4.     for ($i = 0; $i < $zip - > numFiles; $i++) {  
  5.         echo "<li><u>".$zip - > getNameIndex($i).  
  6.         "</u></li>";  
  7.         echo nl2br($zip - > getFromIndex($i)).  
  8.         "<hr/>";  
  9.     }  
  10.     echo "</ol>";  
  11.     $zip - > close();  
  12. }   
 
 
Creating a new zip file or adding contents to an existing zip file
 
A new zip archive can be created using the ZIPARCHIVE::CREATE parameter in the open() function. The addFile() function can be used to add a file to the archive. The following code can be used to create a new zip archive or add files to an existing archive.
  1. $zipfilename = $_GET["zipfilename"];  
  2. $filename = $_GET["filename"];  
  3. $zip = new ZipArchive;  
  4. if ($zip - > open($zipfilename, ZIPARCHIVE::CREATE)) {  
  5.     $zip - > addFile($filename);  
  6.     echo "Added file $filename to zip $zipfilename".  
  7.     "<br/>";  
  8.     $zip - > close();  
  9. else {  
  10.     echo "Cannot create zip file $zipfilename".  
  11.     "<br/>";  
  12. }   
 
 

Conclusion

 
At the end of this short article, I hope people find it useful and get motivated to explore many more things that PHP can do.


Similar Articles