Saturday, April 7, 2018

How to create a zip file using PHP - ZipArchive Class

Today, we are going to discuss how to create a zip file in php. PHP has a ZipArchive class which can be used easily to create zip files. In this tutorial we will create a new zip file, update the existing zip file and moving or placing the files into specific directory or folder.
How to create a zip file using PHP - ZipArchive Class



Lets see the below examples and this may help you to build more understanding on ZipArchive Class of php.


Create a new zip file 

This example explains how to create new zip and place the existing file in directory into it. Follow the below steps to create zip file in php :
Step -1 :
creates an object of the ZipArchive class.
$zip = new ZipArchive;

Step -2 :
Open a file with filename as "skptricks-zip.zip" and specify the second argument as "ZipArchive::CREATE" to create new zip file.
$zip->open('skptricks-zip.zip', ZipArchive::CREATE)

Step-3 :
To add the existing text file (File1.txt and File2.txt) to zip file, we need to call below function.
// Add files to the zip file
    $zip->addFile('File1.txt');
    $zip->addFile('File2.txt');

Step-4 :
Add the existing file (File3.txt) to zip and rename to another name (File5.txt)
// Add File3.txt file to zip and rename it to File5.txt
    $zip->addFile('File3.txt', 'File5.txt');

Step-5 :
Below line of code helps to add a new file into the zip and also it will append the text content in that file. Second argument of the addFromString() function helps to add the text content in the file.
// Add a file newFile.txt file to zip by adding specified text in newFile.txt
    $zip->addFromString('newFile.txt', 'text to be added to the new.txt file');

Step-6:
Closing the file by mentioning the below line.
// All files are added, so close the zip file.
    $zip->close();

Complete example to create a zip file using php :
Create-New-ZIP-File.php
<?php 
$zip = new ZipArchive;
if ($zip->open('skptricks-zip.zip', ZipArchive::CREATE) == TRUE)
{
    // Add files to the zip file
    $zip->addFile('File1.txt');
    $zip->addFile('File2.txt');
 
    // Add File3.txt file to zip and rename it to File5.txt
    $zip->addFile('File3.txt', 'File5.txt');
 
    // Add a file newFile.txt file to zip by adding specified text in newFile.txt
    $zip->addFromString('newFile.txt', 'text to be added to the new.txt file');
 
    // All files are added, so close the zip file.
    $zip->close();
}
?>

Create a new zip file

Overwrite an existing zip file 

To perform overwrite operation in an existing zip file, we have to modify the few line of our existing script. While opening a zip file we have to specify the second argument as "ZipArchive::OVERWRITE".
Lets see the complete source code to perform overwrite operation in a zip file :
Overwrite-Existing-ZIP-File.php
<?php 
$zip = new ZipArchive;
if ($zip->open('skptricks-zip.zip', ZipArchive::OVERWRITE) == TRUE)
{
    // Add files to the zip file
    $zip->addFile('File1.txt');
    $zip->addFile('File2.txt'); 
 
    // All files are added, so close the zip file.
    $zip->close();
}
?>

Overwrite an existing zip file

Create a new zip file and move the files to be in different folders 

Lets see the below source code which helps to create a new zip and place the existing files inside the specific folder or directory.
Move-File-To-Folder.php
<?php 
$zip = new ZipArchive;
if ($zip->open('skptricks-zip.zip',  ZipArchive::CREATE) == TRUE)
{
    // Add files to the zip file
    $zip->addFile('File1.txt', 'Folder1/File1.txt');
    $zip->addFile('File2.txt','Folder2/File2.txt'); 
 
    // All files are added, so close the zip file.
    $zip->close();
}
?>

Create a zip file with all files from a directory

Below script helps to zip all files from the specific directory or folder. Generally this part of script is required during the backup process.
$zip = new ZipArchive;
if ($zip->open('test_dir.zip', ZipArchive::OVERWRITE) === TRUE)
{
    if ($handle = opendir('Folder1'))
    {
        // Add all files inside the directory
        while (false !== ($entry = readdir($handle)))
        {
            if ($entry != "." && $entry != ".." && !is_dir('Folder1/' . $entry))
            {
                $zip->addFile('Folder1/' . $entry);
            }
        }
        closedir($handle);
    }
 
    $zip->close();
}

Create a Zip File With An Empty Folder

Below script helps to create an empty folder inside the zip file.
<?php 
$zip = new ZipArchive;
if ($zip->open('skptricks-zip.zip', ZipArchive::CREATE) == TRUE)
{
   //Add an empty folder
      $zip->addEmptyDir("empty_folder_name");

   // All files are added, so close the zip file.
    $zip->close();
}
?>

Check Zip Extension Is Enabled or Not 

Sometime due to security reasons Zip Extension is disabled, That time we have to provide extra check point in our scrips, so that it will skip the zip file creation process.
We need to include the below lines in our script :
<?php
      //First check whether zip extension is enabled or not
      if(extension_loaded('zip')) {
           $zip = new ZipArchive();
      }
?>

Lets see the complete source code:
<?php
 //First check whether zip extension is enabled or not
 if(extension_loaded('zip')) {
          
       $zip = new ZipArchive;
    if ($zip->open('skptricks-zip.zip', ZipArchive::CREATE) == TRUE)
    {
    // Add files to the zip file
    $zip->addFile('File1.txt');
    $zip->addFile('File2.txt');
 
    // Add File3.txt file to zip and rename it to File5.txt
    $zip->addFile('File3.txt', 'File5.txt');
 
    // Add a file newFile.txt file to zip by adding specified text in newFile.txt
    $zip->addFromString('newFile.txt', 'text to be added to the new.txt file');
 
    // All files are added, so close the zip file.
    $zip->close();
   }
       
 }
?>

Download Link :
https://github.com/skptricks/php-Tutorials/tree/master/How%20To%20Create%20A%20Zip%20File%20Using%20PHP%20-%20ZipArchive%20Class

This is all about zip file creation process using php script.Thank you for reading this article, and if you have any problem, have a another better useful solution about this article, please write message in the comment section.

No comments:

Post a Comment