Using PHP to process zip compressed files ziparchive

Source: Internet
Author: User
Tags php website ziparchive

After PHP5.2, the processing of compressed files is strengthened, and the zip algorithm, including RAR algorithm, gzip algorithm and so on, are supported accordingly.

Today, I'm going to share with you a little bit of PHP processing zip files. We are using the Ziparchive class, and if you have installed more than PHP5.2, you can start using the class without any configuration.

To create a compressed file:

<?php//Instantiation Class $zip = new Ziparchive ();//The zip file that needs to be opened, the file does not exist will be automatically created $filename = "./test.zip"; if ($zip->open ($filename, Ziparchive::create)!==true) {//If it is a Linux system, you need to ensure that the server opens the file Write permission    exit ("File open failed!");} Add a string to the compressed file, the Test.txt file will automatically create $zip->addfromstring ("Test.txt", "Hello, World");//Add the test.php file to the compressed file $zip-> AddFile ("test.php");//output The number of files added, here should be 2echo "number of files:". $zip->numfiles;//Close File $zip->close (); >

The file structure created:

Here's another look at the unzip file:

<?php$zip = new Ziparchive;if ($zip->open (' test.zip ') = = = TRUE) {//The files in Test.zip are extracted to the test folder and the folders are automatically created    $zip- >extractto (' test/');    $zip->close ();    Echo ' decompression succeeded ';} else {    echo ' decompression failed ';}? >



The basic use of the Ziparchive class is so simple. Of course, it also has a lot of powerful features, please refer to the PHP website for relevant content.

Welcome to communicate with me, if you have any questions please leave a message.

Using PHP to process zip compressed files ziparchive

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.