Home > Article > Backend Development > PHP comes with ZIP compression and decompression class ZipArchiv usage guide_PHP tutorial
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:
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:
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:
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:
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:
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:
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:
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:
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:
The above is the entire content of this article, I hope it can be helpful to everyone.