PHPZipArchive implements compression and decompression of Zip files

Source: Internet
Author: User
Tags decompress zip php ziparchive zip extension ziparchive
: This article describes how to compress and decompress a Zip file using PHPZipArchive. For more information about PHP tutorials, see. PHP ZipArchive is a PHP Extension class that can easily compress and decompress ZIP files. before using it, make sure that the php zip extension is enabled, methods for enabling PHP amplification on different platforms are available online. if you have any questions, please contact us. Here we will sort out common examples for your reference.

I. decompress the zip file

$ Zip = new ZipArchive; // Create a ZipArchive object/* process the zip file through the ZipArchive object $ zip-> the parameter of the open method indicates the zip file name to be processed. If the operation on the zip file object is successful, $ zip-> open returns TRUE */if ($ zip-> open('test.zip ') === TRUE) {$ zip-> extracloud ('images'); // suppose you extract the package to the subfolders of the images folder in the current path: php $ zip-> close (); // Close the processed zip file}

2. compress the file into a zip file

$ Zip = new ZipArchive;/* $ zip-> the first parameter of the open method indicates the zip file name to be processed. The second parameter indicates the processing mode. ZipArchive: OVERWRITE indicates that if the zip file exists, the original zip file will be overwritten. If the parameter uses ZIPARCHIVE: CREATE, the system will add content to the original zip file. If you do not want to add content to the zip file multiple times, we recommend that you use ZipArchive: OVERWRITE. If the zip file does not exist, the system automatically creates a new one. If the operation on the zip file object is successful, $ zip-> open returns TRUE */if ($ zip-> open('test.zip ', ZipArchive: OVERWRITE) === TRUE) {$ zip-> addFile('image.txt '); // The hypothetical file name is image.txt. in the current path, $ zip-> close ();}

3. add the content appended to the zip file

$zip = new ZipArchive;$res = $zip->open('test.zip', ZipArchive::CREATE);if ($res === TRUE) {$zip->addFromString('test.txt', 'file content goes here');$zip->close();echo 'ok';} else {echo 'failed';}


4. package folders into zip files
Function addFileToZip ($ path, $ zip) {$ handler = opendir ($ path); // specify $ path to open the current folder. /* Cyclically read all the files and folders in the folder. $ filename = readdir ($ handler) assigns a value to $ filename for each loop, to avoid getting stuck in an endless loop, you need to make $ filename! = False. Be sure to use it! =, Because if the name of a file is '0' or is regarded as false by the system, use! = Will stop the loop */while ($ filename = readdir ($ handler ))! = False) {if ($ filename! = "." & $ Filename! = ".. ") {// The folder name is '. 'and '.. ', do not operate on them if (is_dir ($ path. "/". $ filename) {// recursive addFileToZip ($ path. "/". $ filename, $ zip);} else {// add the file to the zip object $ zip-> addFile ($ path. "/". $ filename) ;}}@ closedir ($ path) ;}$ zip = new ZipArchive (); if ($ zip-> open('images.zip ', ZipArchive: OVERWRITE) === TRUE) {addFileToZip ('images/', $ zip); // call the method to operate the root directory to be packaged, and pass the ZipArchive object to the method $ zip-> close (); // close the processed zip file}

The ZipArchive method is as follows:

  • ZipArchive: addEmptyDir-Add a new directory
  • ZipArchive: addFile-Adds a file to a ZIP archive from the given path
  • ZipArchive: addFromString-Add a file to a ZIP archive using its contents
  • ZipArchive: close-Close the active archive (opened or newly created)
  • ZipArchive: deleteIndex-delete an entry in the archive using its index
  • ZipArchive: deleteName-delete an entry in the archive using its name
  • ZipArchive: extracemedi-extract the archive contents
  • ZipArchive: getArchiveComment-Returns the Zip archive comment
  • ZipArchive: getCommentIndex-Returns the comment of an entry using the entry index
  • ZipArchive: getCommentName-Returns the comment of an entry using the entry name
  • ZipArchive: getFromIndex-Returns the entry contents using its index
  • ZipArchive: getFromName-Returns the entry contents using its name
  • ZipArchive: getNameIndex-Returns the name of an entry using its index
  • ZipArchive: getStatusString-Returns the status error message, system and/or zip messages
  • ZipArchive: getStream-Get a file handler to the entry defined by its name (read only ).
  • ZipArchive: locateName-Returns the index of the entry in the archive
  • ZipArchive: open-Open a ZIP file archive
  • ZipArchive: renameIndex-Renames an entry defined by its index
  • ZipArchive: renameName-Renames an entry defined by its name
  • ZipArchive: setArchiveComment-Set the comment of a ZIP archive
  • ZipArchive: setCommentIndex-Set the comment of an entry defined by its index
  • ZipArchive: setCommentName-Set the comment of an entry defined by its name
  • ZipArchive: statIndex-Get the details of an entry defined by its index.
  • ZipArchive: statName-Get the details of an entry defined by its name.
  • ZipArchive: unchangeAll-Undo all changes done in the archive
  • ZipArchive: unchangeArchive-Revert all global changes done in the archive.
  • ZipArchive: unchangeIndex-Revert all changes done to an entry at the given index
  • ZipArchive: unchangeName-Revert all changes done to an entry with the given name.

  • The above introduces PHP ZipArchive to implement compressed and decompressed Zip files, including some content, hope to be helpful to friends who are interested in PHP tutorials.

    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.