SoFunction
Updated on 2025-03-10

PHP comes with ZIP compression and decompression ZipArchiv usage guide

To use this PHP extension class, it is required (PHP 5 >= 5.2.0, PECL zip >= 1.1.0), some methods require PHP 5.2.+, and the configuration supports zip
For win system, just remove the comments of php_zip.dll extension, and then restart the http service (IIS or Apache).
Linux has not been tested yet, so the difference will not be very big in theory

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

--------------------- Introduction to common methods of ZipArchive objects ---------------------

Testing conventions:
The test file is that the compressed file contains three compressed files (,,), as shown below

Copy the codeThe code is as follows:


    
    
    

Open the 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 create a new file, and 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 attribute, it indicates the corresponding error or returns TRUE
$res == ZipArchive::ER_EXISTS    File already exists. (The file already exists)
$res == ZipArchive::ER_INCONS   Zip archive inconsistent. (Compressed files are inconsistent)
$res == ZipArchive::ER_INVAL    Invalid argument. (Invalid parameter)
$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. (Not a zip archive)
$res == ZipArchive::ER_OPEN       Can't open file. (The file cannot be opened)
$res == ZipArchive::ER_READ          Read error. (Read error)
$res == ZipArchive::ER_SEEK        Seek error. (Find error)

Copy the codeThe code is as follows:

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

Return the name of the compressed file according to the list index in the compressed file

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

Copy the codeThe code is as follows:

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

Get the text stream of the file according to the file name in the compressed

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

Copy the codeThe code is as follows:

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

Modify the file name in the compressed file according to the index (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.

Copy the codeThe code is as follows:

<?php
 $zip = new ZipArchive;
 $res = $zip->open('');
 if ($res === TRUE) {
//Modify the first file in the compressed file into
     $zip->renameIndex(0,'');
     $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)

Copy the codeThe code is as follows:

 <?php
 $zip = new ZipArchive;
 $res = $zip->open('');
 if ($res === TRUE) {
//Modify the compressed file into
     $zip->renameName('','');
     $zip->close();
 } else {
     echo 'failed, code:' . $res;
 }
 ?>

Get the comments of the compressed file (zip's 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 comment that has not been changed
For example, when processing this compressed file, use the setArchiveComment() method to change or set comments
If the parameter ZipArchive::FL_UNCHANGED is added, it means to obtain the comment content before the change, otherwise, obtain the comment content that has been changed.
Similar things are:
ZipArchive::getCommentIndex Get [File Comment] based on the file index in the compressed file
ZipArchive::getCommentName    Get [File Comment] according to the file name in the compressed file
Note: Here are file comments, not comments for compressed files (zip)

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

Copy the codeThe code is as follows:

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

Delete files in the compressed file according to 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()

Copy the codeThe code is as follows:

 $zip = new ZipArchive();

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

That is, package one or more files into a zip file

1. Only need to new one 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 you just created.
4. Finally remember to close the object

Copy the codeThe code is as follows:

<?php
//Create a new ZipArchive object
$zip = new ZipArchive;
 $res = $zip->open('');
//If it is successfully opened
if ($res === TRUE) {
//If the opening fails
} else {
//The output error code
    echo 'failed, code:' . $res;
 }
 $zip->close();

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