Home >Backend Development >PHP Tutorial >Usage examples of php's ZipArchive class, phpziparchive examples_PHP tutorial

Usage examples of php's ZipArchive class, phpziparchive examples_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:16:371319browse

PHP’s ZipArchive class usage example, phpziparchive instance

This article describes the usage of ZipArchive class in PHP and shares it with you for your reference. The details are as follows:

Generally speaking, php5.2 starts to support the ZipArchive class, and php4 can only use the zip function. In fact, before the zip class was officially implemented, some experts had already contributed methods for packaging and decompressing zip files. Now php includes the ZipArchive class, which of course is used first. Using this class, you can create and decompress zip files, and you can also directly read the contents of the zip package, which is very convenient. Here we mainly summarize the reading and decompression process.

Extract a package to the specified directory:

Copy code The code is as follows:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$zip->extractTo('/my/destination/dir/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

If you just need to read the contents of a file in the package, you need the file name or the index value of the file.

Copy code The code is as follows:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
echo $zip->getFromName('example.php');
$zip->close();
}
?>

If example.php is in a certain directory, you need to add the path when retrieving the content.

If you only know the file name but not the specific path of the file, you can search the index of the specified file name and then rely on the index to obtain the content.

Copy code The code is as follows:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
$index=$zip->locateName('example.php', ZIPARCHIVE::FL_NOCASE|ZIPARCHIVE::FL_NODIR);
$contents = $zip->getFromIndex($index);
}
?>

The index obtained above relies on the locateName method. If there are files with the same name in multiple paths in the compressed package, it seems that only the index of the first one can be returned. If you want to get the index of all files with the same name, you can only use a stupid method and search in a loop.

Copy code The code is as follows:
$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
for($i = 0; $i < $zip->numFiles; $i++)
          {
If(substr_count($zip->getNameIndex($i), 'example.php')>0){
                      $contents = $zip->getFromIndex($i);             }
}
}
?>
I hope this article will be helpful to everyone’s PHP programming design.

Can php ZipArchive generate a compressed package in the specified directory?

Of course you can, dear


The following code generates the aaaa.zip file in the /tmp directory
$zip = new ZipArchive(); $zip->open('/tmp/aaaa .zip', ZipArchive::CREATE); $zip->addEmptyDir('dir1'); $zip->close();

For PHP’s zipArchive()

It may be caused by the limitation of PHP's available memory. Try setting the value of the
memory_limit
option in php.ini to a larger value.

can also be set in a php file, for example:

ini_set('memory_limit', '32M');

Additional: If
32M is not enough, add another Add up.
Try using the php.ini file settings.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/897011.htmlTechArticlePHP’s ZipArchive class usage example, phpziparchive example This article describes the use of php’s ZipArchive class, and shares it with everyone for everyone refer to. The details are as follows: Generally speaking, php5.2 starts to support...
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