PHP extension compression and archive extension 3-Zip this extension allows you to read and write ZIP compressed documents and their files transparently.
To use these functions after PHP5.2, you must use the -- enable-zip configuration option to provide zip support during PHP compilation.
Example #1 create a Zip archive
open($filename, ZIPARCHIVE::CREATE)!==TRUE) { exit("cannot open <$filename>\n"); } $zip->addFromString("testfilephp.txt" . time(), "#1 This is a test string added as testfilephp.txt.\n"); $zip->addFromString("testfilephp2.txt" . time(), "#2 This is a test string added as testfilephp2.txt.\n"); $zip->addFile($thisdir . "/too.php","/testfromfile.php"); echo "numfiles: " . $zip->numFiles . "\n"; echo "status:" . $zip->status . "\n"; $zip->close();?>
Example #2 output document details and list
open('test_with_comment.zip'); print_r($za); var_dump($za); echo "numFiles: " . $za->numFiles . "\n"; echo "status: " . $za->status . "\n"; echo "statusSys: " . $za->statusSys . "\n"; echo "filename: " . $za->filename . "\n"; echo "comment: " . $za->comment . "\n"; for ($i=0; $i<$za->numFiles;$i++) { echo "index: $i\n"; print_r($za->statIndex($i)); } echo "numFile:" . $za->numFiles . "\n";?>
Example #3 Zip stream encapsulation, read the metadata of an OpenOffice file
open('zip://' . dirname(__FILE__) . '/test.odt#meta.xml'); $odt_meta = array(); while ($reader->read()) { if ($reader->nodeType == XMLREADER::ELEMENT) { $elm = $reader->name; } else { if ($reader->nodeType == XMLREADER::END_ELEMENT && $reader->name == 'office:meta') { break; } if (!trim($reader->value)) { continue; } $odt_meta[$elm] = $reader->value; } } print_r($odt_meta);?>
In this example, the old API (PHP 4) is used to open a ZIP file archive, read each file in the archive, and output the file content. The test2.zip document used in this example is one of the test documents in ZZIPlib source distribution.
Example #4 Example of Zip
ZipArchive class
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: addGlob-Add files from a directory by glob pattern
ZipArchive: addPattern-Add files from a directory by PCRE pattern
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.
Related functions
Zip_close-Close a ZIP file archive
Zip_entry_close-Close a directory entry
Zip_entry_compressedsize-Retrieve the compressed size of a directory entry
Zip_entry_compressionmethod-Retrieve the compression method of a directory entry
Zip_entry_filesize-Retrieve the actual file size of a directory entry
Zip_entry_name-Retrieve the name of a directory entry
Zip_entry_open-Open a directory entry for reading
Zip_entry_read-Read from an open directory entry
Zip_open-Open a ZIP file archive
Zip_read-Read next entry in a ZIP file archive