Home > Backend Development > PHP Tutorial > PHP comes with ZIP compression and decompression class ZipArchiv usage guide_PHP tutorial

PHP comes with ZIP compression and decompression class ZipArchiv usage guide_PHP tutorial

WBOY
Release: 2016-07-13 10:04:56
Original
1178 people have browsed it

PHP’s own ZIP compression and decompression class ZipArchiv usage guide

This article mainly introduces PHP’s own ZIP compression and decompression class ZipArchiv usage guide. It is very detailed and requires Friends can refer to it

To use this PHP extension class, you need (PHP 5 >= 5.2.0, PECL zip >= 1.1.0), some methods require PHP 5.2.+, and the php.ini configuration supports zip
For win system, just remove the comment of php_zip.dll extension, and then restart the http service (IIS or Apache)
Linux has not been tested yet, and theoretically the difference will not be big

Function:
1. Unzip the zip file
2. Compress the file into a zip file
3. Append files to zip file
4. Pack the folder into a zip file (need to add files and create empty folders in a loop)
5. Delete entries in the compressed file

--------------------- Introduction to common methods of ZipArchive object---------------------

Testing convention:
The test file is text.zip, which contains three compressed files (hello.txt, word.txt, ooxx.jpg), as shown below

The code is as follows:


text.zip
hello.txt
word.txt
ooxx.jpg

Open zip file for further operation
ZipArchive::open
(PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
mixed ZipArchive::open ( string $filename [, int $flags ] )

Explanation of the second parameter

ZIPARCHIVE::OVERWRITE always creates a new file. If the specified zip file exists, it will be overwritten
ZIPARCHIVE::CREATE If the specified zip file does not exist, create a new one
ZIPARCHIVE::EXCL If the specified zip file exists, an error will be reported
ZIPARCHIVE::CHECKCONS

Return value:

If the return value is equal to the following properties, it indicates the corresponding error or returns TRUE
$res == ZipArchive::ER_EXISTS File already exists. (File already exists)
$res == ZipArchive::ER_INCONS Zip archive inconsistent. (compressed files are inconsistent)
$res == ZipArchive::ER_INVAL Invalid argument.
$res == ZipArchive::ER_MEMORY Malloc failure. (Memory error? This is not sure)
$res == ZipArchive::ER_NOENT No such file. (No such file)
$res == ZipArchive::ER_NOZIP Not a zip archive. (There is no compressed file)
$res == ZipArchive::ER_OPEN Can't open file.
$res == ZipArchive::ER_READ Read error.
$res == ZipArchive::ER_SEEK Seek error.

The code is as follows:


$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
echo 'ok';
//Extract to test folder
$zip->extractTo('test');
$zip->close();
} else {
echo 'failed, code:' . $res;
}
?>

Returns the name of the compressed file based on the list index within the compressed file

ZipArchive::getNameIndex
string ZipArchive::getNameIndex ( int $index [, int $flags ] )

The code is as follows:


$zip = new ZipArchive();
$res = $zip->open('test.zip');
if ($res === TRUE) {
var_dump($zip->getNameIndex(0)); // hello.txt
var_dump($zip->getNameIndex(1)); // word.txt
var_dump($zip->getNameIndex(2)); // ooxx.jpg
} else {
echo 'failed, code:' . $res;
}
$zip->close();
?>

According to the file name in the compression, get the text stream of the file

ZipArchive::getStream
resource ZipArchive::getStream ( string $name )

The code is as follows:


$zip = new ZipArchive();
$res = $zip->open('test.zip');
if ($res === TRUE) {
$stream = $zip->getStream('hello.txt');
} else {
echo 'failed, code:' . $res;
}
$zip->close();
$str = stream_get_contents($stream); //Pay attention to the obtained text encoding here
var_dump($str);
?>

Modify the file name in the compressed file according to the index in the compressed file (starting from 0)

ZipArchive::renameIndex
bool ZipArchive::renameIndex ( int $index , string $newname )
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

Returns TRUE on success, or FALSE on failure.

The code is as follows:


$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
//Modify the first file in the compressed file to newname.txt
$zip->renameIndex(0,'newname.txt');
$zip->close();
} else {
echo 'failed, code:' . $res;
}
?>

Modify the file name in the compressed file according to the file name in the compressed file

ZipArchive::renameName
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

The code is as follows:


$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
//Modify word.txt in the compressed file to newword.txt
$zip->renameName('word.txt','newword.txt');
$zip->close();
} else {
echo 'failed, code:' . $res;
}
?>

Get comments of compressed files (zip file comments)

ZipArchive::getArchiveComment
(PHP 5 >= 5.2.0, PECL zip >= 1.1.0)
string ZipArchive::getArchiveComment ([ int $flags ] )
Parameters: ZipArchive::FL_UNCHANGED
If the parameter is set to ZipArchive::FL_UNCHANGED, return the original unchanged annotation
For example, when processing the compressed file, use the setArchiveComment() method to change or set the comment
If you add the parameter ZipArchive::FL_UNCHANGED, it means to get the annotation content before the change, otherwise, get the changed annotation content
Similar ones include:
ZipArchive::getCommentIndex Gets [file comment] based on the file index in the compressed file
ZipArchive::getCommentName Gets [file comment] based on the file name in the compressed file
Note: These are file comments, not compressed file (zip) comments

Set or modify comments of compressed files (zip file comments)
ZipArchive::setArchiveComment
(PHP 5 >= 5.2.0, PECL zip >= 1.4.0)
bool ZipArchive::setArchiveComment ( string $comment )

The code is as follows:


$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
//$zip->addFromString('test.txt', 'file content goes here');
$zip->setArchiveComment('new archive comment');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>

Delete files in the compressed file based on the index in the compressed file (that is, delete entries in the archive)

ZipArchive::deleteIndex
(PHP 5 >= 5.2.0, PECL zip >= 1.5.0)

1. How to decompress a zip file extractTo()

The code is as follows:


$zip = new ZipArchive();

1. How to create a compressed file? addFromString() addFile()

That is to pack one or more files into a zip file

1. Only need a new ZipArchive object
2. Then use the open method of the object to create a zip file
3. Then use the addFile method to write the file to be packaged into the zip file just created
4. Finally, remember to close the object

The code is as follows:


//Create a new ZipArchive object
$zip = new ZipArchive;
$res = $zip->open('test.zip');
//If the opening is successful
if ($res === TRUE) {
//If opening fails
} else {
//Output the error code
echo 'failed, code:' . $res;
}
$zip->close();

The above is the entire content of this article, I hope it can be helpful to everyone.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/964003.htmlTechArticlePHP’s own ZIP compression and decompression class ZipArchiv usage guide This article mainly introduces PHP’s own ZIP compression, Decompression ZipArchiv usage guide, very detailed, friends in need can refer to it...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template