Compressing and Encrypting File with PHP Zip Module

Source: Internet
Author: User
Keywords php zip module encrypting files php php compress files
Tags php ziparchive ziparchive

Objective

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.


Begin


$zipArc = new \ZipArchive();

If ($zipArc->open('/home/test.zip', ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {

     / / Set the password Note that this is not encrypted, just set the password
     If (!$zipArc->setPassword('password')) {
         Throw new RuntimeException('Set password failed');
     }

     / / Add files to the compressed package
     $zipArc->addFile('/home/test.png', '1/test.png');

     / / Encrypt the file Here the file name and path is in the compressed package
     If (!$zipArc->setEncryptionName('1/test.png', ZipArchive::EM_AES_256)) {
         Throw new RuntimeException('Set encryption failed');
     }

}

$zipArc->close();

Precautions

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

--with-libzip
--enable-zip
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 is the file path to be added $b is the path inside the compressed package
$zipArc->addFile($a, $b);
Resources
    • PHP ziparchive compressed folder, ZIP file directory structure is not
    • Official document Ziparchive Class

PHP Encrypted Compressed file

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: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.