Introduction to Working with ZIP Archives in PHP
ZIP archives remain one of the most popular data compression formats in web development. In PHP, working with them is implemented natively through the Zip extension, which provides the powerful ZipArchive class. This allows developers to easily create, read, update, and extract files from archives without installing additional third-party libraries.
On the educational portal PythonLib.ru, we will cover all aspects of working with ZIP in PHP: from basic operations to advanced techniques, including password-protected compression, handling large volumes of data, and integration with output buffering.
Installing and Checking the Zip Extension
The ZipArchive class is available in PHP starting from version 5.2.0. To use it, you need to ensure that the zip extension is enabled in the server configuration. You can check this using the phpinfo() function or a simple script:
<?phpif (class_exists('ZipArchive')) { echo "Zip extension is available";} else { echo "Zip extension is not installed";}?>On most modern hosting platforms, the extension is already pre-installed. If it is not, contact your server administrator or install it via a package manager (e.g., apt-get install php-zip for Debian/Ubuntu).
Basic Operations with ZipArchive
1. Creating a New ZIP Archive
To create an archive, use the open() method with the ZipArchive::CREATE flag. Consider a basic example:
<?php$zip = new ZipArchive();$filename = 'example.zip';
if ($zip->open($filename, ZipArchive::CREATE) === TRUE) { // Adding a file from a string $zip->addFromString('hello.txt', 'Hello, world!'); // Adding an existing file $zip->addFile('/path/to/image.jpg', 'images/photo.jpg'); // Adding an empty directory $zip->addEmptyDir('documents'); $zip->close(); echo "Archive created successfully";} else { echo "Error creating archive";}?>Note: the second parameter of addFile() specifies the path inside the archive, allowing you to organize the folder structure.
2. Extracting a ZIP Archive
To extract all files, use the extractTo() method:
<?php$zip = new ZipArchive();if ($zip->open('example.zip') === TRUE) { $zip->extractTo('/destination/folder/'); $zip->close(); echo "Archive extracted";} else { echo "Failed to open archive";}?>You can also extract only a specific file by specifying its internal path as the second argument:
$zip->extractTo('/destination/', 'images/photo.jpg');3. Reading Archive Contents Without Extraction
Sometimes you only need to view the list of files inside the archive:
<?php$zip = new ZipArchive();if ($zip->open('archive.zip') === TRUE) { for ($i = 0; $i < $zip->numFiles; $i++) { $stat = $zip->statIndex($i); echo "File: " . $stat['name'] . " (size: " . $stat['size'] . " bytes)"; } $zip->close();}?>The statIndex() method returns an array with information: name, size, modification date, CRC32, and other metadata.
Advanced Features
Password-Protected Compression (PHP 7.2+)
Starting from PHP 7.2, ZipArchive supports password encryption using the AES-256 algorithm:
<?php$zip = new ZipArchive();if ($zip->open('protected.zip', ZipArchive::CREATE) === TRUE) { $zip->setPassword('supersecret'); $zip->addFile('document.pdf'); // Enable encryption for the last added file $zip->setEncryptionName('document.pdf', ZipArchive::EM_AES_256); $zip->close(); echo "Encrypted archive created