Home > Article > Backend Development > Usage analysis of ZipArchive method for php packaged compressed files
The example in this article describes the use of ZipArchive method for packaging compressed files in PHP. Share it with everyone for your reference, the details are as follows:
I mentioned the PclZip method of php packaging and compressing files. Today I will talk about another simpler method, using ZipArchive to compress files. This is an extension class of php. This extension has been supported since php5.2 version. If you get an error when using it, check whether the semicolon in front of extension=php_zip.dll in php.ini has been removed, and then try again. Restart Apache to use this class library.
It is very simple to use ZipArchive to compress files. The official php website has provided me with many related examples. You can take a look at http://www.php.net/manual/zh/class.ziparchive.php, for example:
<?php $zip = new ZipArchive; //首先实例化这个类 if ($zip->open('test.zip') === TRUE) { //然后查看是否存在test.zip这个压缩包 $zip->addFile('too.php'); $zip->addFile('test.php'); //将too.php和test.php两个文件添加到test.zip压缩包中 $zip->close(); //关闭 echo 'ok'; } else { echo 'failed'; } ?>
The following are some parameter descriptions:
<?php /******** ziparchive 可选参数 *******/ /* 1.ZipArchive::addEmptyDir 添加一个新的文件目录 2.ZipArchive::addFile 将文件添加到指定zip压缩包中。 3.ZipArchive::addFromString 添加的文件同时将内容添加进去 4.ZipArchive::close 关闭ziparchive 5.ZipArchive::extractTo 将压缩包解压 6.ZipArchive::open 打开一个zip压缩包 7.ZipArchive::getStatusString 返回压缩时的状态内容,包括错误信息,压缩信息等等 8.ZipArchive::deleteIndex 删除压缩包中的某一个文件,如:deleteIndex(0)删除第一个文件 9.ZipArchive::deleteName 删除压缩包中的某一个文件名称,同时也将文件删除。 ...... */ ?>
In short, as long as your php5.2 version or above is used, it is more convenient to use ZipArchive for file compression. Specifically, which one runs faster than PclZip? This Lezi has not been tested yet. If you are interested, you can play around with it.
Readers who are interested in more PHP-related content can check out the special topics of this site: "Summary of PHP operation zip files and compression techniques", "Summary of PHP file operation", "Summary of PHP regular expression usage", "PHP operations and operations" Summary of Symbol Usage", "Introduction Tutorial on PHP Basic Syntax", "Introduction Tutorial on PHP Object-Oriented Programming", "Summary of PHP String Usage", "Introduction Tutorial on PHP+MySQL Database Operation" and "PHP Common Database Operation Skills" Summary》
I hope this article will be helpful to everyone in PHP programming.
The above has introduced the usage analysis of ZipArchive method for packaging compressed files in PHP, including the relevant aspects. I hope it will be helpful to friends who are interested in PHP tutorials.