Current Location: Home> Latest Articles> Complete Guide to Using PHP ZipArchive for Folder Compression and Extraction

Complete Guide to Using PHP ZipArchive for Folder Compression and Extraction

M66 2025-07-07

How to Use PHP ZipArchive for Folder Compression and Extraction?

In PHP, the ZipArchive class is a powerful tool that makes it easy to handle folder compression and extraction. This article will walk you through how to use the ZipArchive class to achieve these tasks, with practical code examples provided.

Folder Compression

Folder compression is the process of packaging a folder and all its contents into a single zip file. PHP's ZipArchive class provides a straightforward way to accomplish this.

First, we create an instance of the ZipArchive class and open or create a zip file using the open method:

$zip = new ZipArchive();
$zip->open('path/to/archive.zip', ZipArchive::CREATE);

Next, we add the files to be compressed using the addGlob method:

$zip->addGlob('path/to/files/*');

The wildcard “*” matches all files inside the specified folder.

After adding the files, we close the zip file using the close method to complete the compression:

$zip->close();

The complete code example is as follows:

$zip = new ZipArchive();
$zip->open('path/to/archive.zip', ZipArchive::CREATE);
$zip->addGlob('path/to/files/*');
$zip->close();

Folder Extraction

Folder extraction is the process of extracting a zip file to a specified directory. Again, the ZipArchive class makes this task straightforward.

First, open the zip file using the open method:

$zip = new ZipArchive();
$zip->open('path/to/archive.zip');

Then, use the extractTo method to extract the contents of the zip file to the desired destination:

$zip->extractTo('path/to/destination');

Finally, use the close method to close the zip file and complete the extraction:

$zip->close();

The complete code example is as follows:

$zip = new ZipArchive();
$zip->open('path/to/archive.zip');
$zip->extractTo('path/to/destination');
$zip->close();

Error Handling

While working with the ZipArchive class for compression and extraction, you may encounter errors. To ensure robust code, you can use the getStatusString method to retrieve detailed error messages and troubleshoot accordingly.

Here's an example of error handling:

$zip = new ZipArchive();
if ($zip->open('path/to/archive.zip') === true) {
    $zip->extractTo('path/to/destination');
    $zip->close();
} else {
    echo 'Failed to open archive: ' . $zip->getStatusString();
}

Using the error messages returned by getStatusString, you can easily locate and resolve the issue.

Conclusion

The PHP ZipArchive class provides an efficient way to handle folder compression and extraction. Using methods like open, addGlob, extractTo, and close, you can easily manage these tasks. When implementing this functionality, it's important to handle errors properly and adjust the code as needed.

This concludes our detailed guide on using PHP ZipArchive for folder compression and extraction. We hope this article was helpful to you!