In a recent blog post, Remi Collet pointed to the latest development of the PHP Zip Archive extension which now support encryption. New features are still experimental and might change, so it’s not yet ready for production. The new feature implementation is based on libzip library new version 1.2.0, and will be shipped with Zip extension version 1.14.0 and most probably with PHP 7.2 as the code is already available in the php_master. You can install the new experimental features from remi’s test RPM or from sources in github :

$ phpize
$ ./configure --with-libzip
...
checking for libzip... from pkgconfig: version 1.2.0 found in /usr/lib64
checking for zip_open in -lzip... yes
checking for zip_file_set_encryption in -lzip... yes
...
$ make
...
Build complete.
Don't forget to run 'make test'.
$ make test
...
PASS ZipArchive::setEncryption*() functions [tests/oo_encryption.phpt]

Three methods are available to manage encryption in PHP Zip :

ZipArchive::setEncryptionName($name, $method [, $password]);
ZipArchive::setEncryptionIndex($index, $method [, $password]);
ZipArchive::setPassword($password);

Encryption method being one of the new constants:  ZipArchive::EM_NONE, ZipArchive::EM_AES_128, ZipArchive::EM_AES_192 or ZipArchive::EM_AES_256. The new implementation support adding a per file password or using a default global password. To encrypt an archive you can write the following code :

$zip = new ZipArchive;
$zip->open(__DIR__ . '/encrypted.zip');
print_r($zip->statName($file));
$zip->setPassword('secret');
$text = $zip->getFromName('foo.php');
$zip->close();

This new feature seems really useful and will improve compatibility with other tools, such as WinZip for Windows or 7za for Linux.

LEAVE A REPLY

Please enter your comment!
Please enter your name here