How to zip and unzip files in PHP

Zip and Unzip Files in PHP helps reduce file size, saving time and bandwidth when transferring data over the internet. Compressing and extracting files makes file handling faster and more efficient.

PHP provides built-in extensions for file compression and extraction, so you don’t need extra plugins. Using the ZipArchive class, you can easily create zip files or extract existing ones, with various methods available for managing zip archives.

In this tutorial, we will go through practical examples of Zip and Unzip Files in PHP.

Unzip Files

To unzip the zip file, we will use the extractTo() method provided by the ZipArchive class. This function takes two parameters which are destination and files. We will use the second parameter when we want to extract specific files.

Let’s take an example to extract all files or specific files.

<?php
    $zip = new ZipArchive;

    if ($zip->open('images.zip') === TRUE) {    //Zip file name
        // To extract all files
        $zip->extractTo('/uploads/images/');    //Destination location

        //To extract specific file
        $zip->extractTo('/uploads/images/', 'file.txt');    //Destination location
        $zip->close();

        echo 'Success';
    } else {
        echo 'Failed';
    }
?>

In the above example, we have created an object of the ZipArchive class, and then we try to open a zip file and print a message according to it. After that, we will use the extractTo() method and pass the destination folder as a parameter. Use a second way to extract specific files. Here, you can pass an array with the names of files or directories.

Create Zip File

We can add files to the zip archive one at a time or add the whole directory at once. In either case, the first step is to create a new ZipArchive instance and then call the open() method.

<?php
    // Create new zip class
    $zip = new ZipArchive;

    if($zip -> open('file.zip', ZipArchive::CREATE ) === TRUE) {

        $dir = opendir('test'); //Directory Name

        //Loop through dir and add files one by one
        while($file = readdir($dir)) {
            if(is_file($pathdir.$file)) {
                $zip -> addFile($pathdir.$file, $file);
            }
        }
        $zip ->close();
    }
?>

In the above example, we created a new ZipArchive class object. Then we opened our directory using the opendir command. For adding files to our zip archive we have to loop through all files of the selected directory and add them one by one. Lastly, the close method will close the ZipArchive object and create a zip file in our file system.

Conclusion

In this article, we have compressed and extracted a zip file in PHP using the ZipArchive class. Now You should be able to compress or extract individual files or a group of them at once as per your requirement.

For managing files on a server directly from PHP, Download and Upload Files Through FTP in PHP shows how to transfer files efficiently using FTP, making it easier to handle remote file operations in your projects.