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