Create a Zip File Using PHP

  • Avinash
  • 4
  • Sep 04, 2011
  • Tips & Tricks

I am not sure about other programming language but for PHP yes its quite a simple job to create a zip file. You can say as simple as you create on your machine.

PHP has a very useful class ZipArchive to create and manipulate the .Zip files. In this post I will show how to create and extract .zip files using PHP.

Here are the function which we will use for this tutorial. open() for open/create any zip file, addFile() for adding the files to archieve, close() for safely close the zip file and extractTo() for extracting the .zip file.

How to create .zip File using PHP

// Creating object of the ZipArchive $zip = new ZipArchive(); $ow = 1; $file= "master.zip"; if($zip->open($file,$ow?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE) { // Add the files to the .zip file $zip->addFile("master.css"); // This means style.css will be added as new_style.css $zip->addFile("style.css", "new_style.css"); $zip->addFile("fonts.css"); // Closing the zip file $zip->close(); // Above code will generate master.zip // containing master.css, new_style.css, fonts.css }
 // Creating object of the ZipArchive
 $zip = new ZipArchive();
 
 $ow = 1;
 $file= "master.zip";
 if($zip->open($file,$ow?ZIPARCHIVE::OVERWRITE:ZIPARCHIVE::CREATE)===TRUE)
 {
   // Add the files to the .zip file
   $zip->addFile("master.css");
   
   // This means style.css will be added as new_style.css
   $zip->addFile("style.css", "new_style.css");
   
   $zip->addFile("fonts.css");
   
   // Closing the zip file
   $zip->close();
   
   // Above code will generate master.zip
   // containing master.css, new_style.css, fonts.css
 }

Extract .zip file using PHP

// Creating object of the ZipArchive $zip = new ZipArchive(); // Open master.zip for extracting all files if ($zip->open("master.zip") === TRUE) { // Will extract all files from master.zip to given path. $zip->extractTo("/path/to/folder/"); $zip->close(); } // Extract only several files // Open master.zip for extracting single files if ($zip->open("master.zip") === TRUE) { // Will extract only fonts.css from master.zip to given path. $zip->extractTo("/path/to/folder/","fonts.css"); $zip->close(); } // Open master.zip for extracting multiple files if ($zip->open("master.zip") === TRUE) { // Will extract only fonts.css and master.css // from master.zip to given path. $files = array("fonts.css","master.css"); $zip->extractTo("/path/to/folder/",$files); $zip->close(); }
 // Creating object of the ZipArchive
 $zip = new ZipArchive();

 // Open master.zip for extracting all files
 if ($zip->open("master.zip") === TRUE)
 {
   // Will extract all files from master.zip to given path.
   $zip->extractTo("/path/to/folder/");
   $zip->close();
 }

 // Extract only several files
 
 // Open master.zip for extracting single files
 if ($zip->open("master.zip") === TRUE)
 {
   // Will extract only fonts.css from master.zip to given path.
   $zip->extractTo("/path/to/folder/","fonts.css");
   $zip->close();
 }

 // Open master.zip for extracting multiple files
 if ($zip->open("master.zip") === TRUE)
 {
   // Will extract only fonts.css and master.css
   // from master.zip to given path.
   $files = array("fonts.css","master.css");
   $zip->extractTo("/path/to/folder/",$files);
   $zip->close();
 }

Related Posts

Written by Avinash

Avinash Zala is leading various projects which deals with the various technology involved with the web. A combination of perfect technical and management skills. Avinash would like to chat with you and convert your imagination into the working system. You can get in touch with him on Facebook and Twitter.

View all posts by:

  • Pingback: Elegant D » Create a Zip File Using PHP | Expert PHP Developer

  • Name

    I was trying to read and understand this post and I’ve found that it is really that significant and useful to everyone. I have a little knowledge abot some programming language but for PHP yes its quite a simple job to create a zip file. You can say as simple as you create on your machine.

  • http://bestgoldfishcare.com goldfishcare

    PHP is my favorite language programming, because use web base design, i like it, and your blog give inspiration to my knowledge of PHP , thank you

  • singar

    thanks avinash for your excellent self explanatory zip code. it is really useful for me
    thanks a lot