此擴充功能可以讓你透明地讀寫ZIP壓縮文件以及它們裡面的檔案。
在PHP5.2以後,為了使用這些函數,必須在編譯 PHP 時用 --enable-zip 設定選項來提供 zip 支援。
Example #1 建立一個Zip 歸檔
<?php $zip = new ZipArchive(); $filename = "./test112.zip"; if ($zip->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 輸出文檔細節和列表
<?php $za = new ZipArchive(); $za->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 流封裝,讀取一個OpenOffice 文件的元資訊
Example #3 Zip 流封裝,讀取一個OpenOffice 文件的元資訊Example #3 Zip 流封裝,讀取一個OpenOffice 文件的元資訊
4),它打開了一個ZIP 文件歸檔,讀取歸檔裡的每個文件,並輸出文件內容。此例用到的 test2.zip 文檔是 ZZIPlib 來源分佈裡測試文檔中的一個。 Example #4 Zip 使用範例<?php $reader = new XMLReader(); $reader->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); ?>
ZipArchive: FromString — Add a file to a ZIP archive using its contents
ZipArchive::addGlob — Add files from a directory by glob pattern
ZipArchive::addr (opened or newly created)
ZipArchive::deleteIndex — delete an entry in the archive using its index
ZipArchive::deleteName — delete an entry in the archivecive 集鑎
ZipArchive::getArchiveComment — Returns the Zip archive comment
ZipArchive::getCommentIndex — Returns the comment of an entry using the entry index👜
ZipArchive:: getFromIndex — Returns the entry contents using its indexZipArchive::getFromName — Returns the entry contents using its nameZipArchive::sNameIndex String — Returns the status error message, system and/or zip messagesZipArchive::getStream — Get a file handler to the entry defined by its name (read only).ZipArchive::locateName Returns the index of theZipArchive::locateName Returns the index of the ipory ::open — Open a ZIP file archive
ZipArchive::renameIndex — Renames an entry defined by its index
ZipArchive::renameName — Renames an entry defined comfive nameo five notoiicom fion sotry defincom IP archive
ZipArchive::setCommentIndex — Set the comment of an entry defined by its index
ZipArchive::setCommentName — Set the comment of an entry defined by its defintail anmepopi Htails metail metail by ed by its index.
ZipArchive::statName — Get the details of an entry defined by its name.
ZipArchive::unchangeAll — Undo all changes .
ZipArchive::unchangeIndex — Revert all changes done to an entry at the given index
ZipArchive::unchangeName — Revert all changes done to an entry with the gigi name. archive
zip_entry_close — Close a directory entry
zip_entry_compressedsize — Retrieve the compressed size of a directory entry
zips_pry_compression ry_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
z 🜥 ZIP file archive