This extension allows you to transparently read and write ZIP archives and the files inside them.
After PHP5.2, in order to use these functions, you must use the --enable-zip configuration option when compiling PHP to provide zip support.
Example #1 Create a Zip archive
<?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 Output document details and list
<?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 stream encapsulation, read the meta information of an OpenOffice file
<?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); ?>
This example uses the old API (PHP 4), it opens a ZIP file archive, reads each file in the archive, and outputs the file contents. The test2.zip file used in this example is one of the test files in the ZZIPlib source distribution.
Example #4 Zip usage example
<?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 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::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.
Related functions
zip_close — Close a ZIP file archive
zip_entry_close — Close a directory entry
zip_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

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor
