在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 文件的元信息
<?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); ?>
此例使用了旧的 api(php 4),它打开了一个 zip 文件归档,读取归档里的每个文件,并输出文件内容。此例用到的 test2.zip 文档是 zziplib 源分布里测试文档中的一个。
example #4 zip 使用范例
<?php $zip = zip_open("/tmp/test2.zip"); if ($zip) { while ($zip_entry = zip_read($zip)) { echo "name: " . zip_entry_name($zip_entry) . "\n"; echo "actual filesize: " . zip_entry_filesize($zip_entry) . "\n"; echo "compressed size: " . zip_entry_compressedsize($zip_entry) . "\n"; echo "compression method: " . zip_entry_compressionmethod($zip_entry) . "\n"; if (zip_entry_open($zip, $zip_entry, "r")) { echo "file contents:\n"; $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); echo "$buf\n"; zip_entry_close($zip_entry); } echo "\n"; } zip_close($zip); } ?>
ziparchive 类
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::extractto — 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.
相关函数
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
