Home  >  Article  >  Backend Development  >  Usage analysis of ZipArchive method for packaging compressed files in php_php skills

Usage analysis of ZipArchive method for packaging compressed files in php_php skills

WBOY
WBOYOriginal
2016-05-16 19:53:261108browse

The example in this article describes the use of the ZipArchive method of php packaging compressed files. Share it with everyone for your reference, the details are as follows:

We talked about the PclZip method of packaging compressed files in PHP. Today we 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:

<&#63;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';
}
&#63;>

Here are some parameter descriptions:

<&#63;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
删除压缩包中的某一个文件名称,同时也将文件删除。
......
*/
&#63;>

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 on this site: "Summary of PHP operation zip files and compression techniques", "Summary of PHP file operation", " Summary of php regular expression usage", "Summary of PHP operations and operator usage", "PHP basic syntax introductory tutorial", "php oriented Introductory Tutorial on Object Programming ", "Summary of PHP String Usage", "Introductory Tutorial on PHP MySQL Database Operations " and "php Common Database Operations" Summary of skills

I hope this article will be helpful to everyone in PHP programming.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn