PHP Encrypted Compressed file

Source: Internet
Author: User

Tags: libz interpreting data Volume custom support structure--folder png


In recent days, some data collected by crawlers, stored in the hard disk, with more and more data, so want to upload to the network, but do not add the secret and feel not at ease,
So we started compressing and encrypting with PHP's Zip module.

$zipArc = new \ZipArchive();if ($zipArc->open('/home/', ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {    //设置密码 注意此处不是加密,仅仅是设置密码    if (!$zipArc->setPassword('password')) {        throw new RuntimeException('Set password failed');    }    //往压缩包内添加文件    $zipArc->addFile('/home/test.png', '1/test.png');    //加密文件 此处文件名及路径是压缩包内的    if (!$zipArc->setEncryptionName('1/test.png', ZipArchive::EM_AES_256)) {        throw new RuntimeException('Set encryption failed');    }}$zipArc->close();


1 PHP7.2 is not supported under encryption

php7.2 The following are not supported for encryption, let's take a look at the explanations in the official PHP documentation

Starting with PHP 7.2.0 and Libzip 1.2.0, the password is used to decompress the archive, which is also the default password for ziparchive:: Setencryptionname () and ziparchive:: Setencryptionindex ().

Previously, this feature only set the password for extracting the archive; It does not turn non-password-protected ziparchive into a password-protected ziparchive.

That is, before php7.2, SetPassword (' password ') This method is only to set the Setencryptionname () and Setencryptionindex () the default password, but no encryption operation!!!, asked you pit !!

2 Method not found

When you are prompted that there is no setencryptionname and Setencryptionindex method, compile the Zip module with the following parameters


3 Directory structure issues

The file directory to be compressed, than to say/home/test/a.png
After compression, you find that the directory structure inside the package is/home/test/a.png,
That is, the compressed package remains intact in the directory of the original file. But what do we do when we want to customize the zipped package Catalog?

$a = '/home/test.png';$b = '1/test.png';//$a是待添加的文件路径  $b是压缩包内的路径$zipArc->addFile($a, $b);

    • PHP ziparchive compressed folder, ZIP file directory structure is not
    • Official document Ziparchive Class

PHP Encrypted Compressed file

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: