PHP - Zip
The Zip extension enables you to transparently read or write ZIP compressed archives and the files inside them.
Installation
Linux Systems
As of PHP 7.4.0, in order to use these functions PHP must be compiled with zip support by using the --with-zip configure option. Previously, zip support had to be enabled by using the --enable-zip configure option. As of PHP 7.4.0, the bundled libzip is removed.
As of PHP 7.3.0, building against the bundled libzip is discouraged, but still possible by adding --without-libzip to the configuration.
A --with-libzip=DIR configure option has been added to use a system libzip installation. libzip version 0.11 is required, with 0.11.2 or later recommended.
Windows
On Windows, this extension is built-in.
Runtime Configuration
This extension has no configuration directives defined in php.ini.
PHP ZipArchive Class
Class Description | |
---|---|
ZipArchive | The ZipArchive class. |
Methods | Description |
close() | Close the active archive (opened or newly created). |
count() | Counts the number of files in the archive. |
open() | Open a ZIP file archive. |
PHP Zip Functions
Functions | Description |
---|---|
zip_close() | Close a ZIP file archive. |
zip_open() | Open a ZIP file archive. |
zip_read() | Read next entry in a ZIP file archive. |
PHP Zip Constants
The constants below are defined by this extension, and will only be available when the extension has either been compiled into PHP or dynamically loaded at runtime.
ZipArchive uses class constants. There are three types of constants : Flags (prefixed with FL_), errors (prefixed with ER_) and mode (no prefix).
Constants | Type | Description |
---|---|---|
ZipArchive::CREATE | Integer | Create the archive if it does not exist. |
ZipArchive::OVERWRITE | Integer | If archive exists, ignore its current contents. In other words, handle it the same way as an empty archive. |
ZipArchive::EXCL | Integer | Error if archive already exists. |
ZipArchive::RDONLY | Integer | Open archive in read only mode. Available as of PHP 7.4.3 and PECL zip 1.17.1, respectively, if built against libzip ≥ 1.0.0. |
ZipArchive::CHECKCONS | Integer | Perform additional consistency checks on the archive, and error if they fail. |
ZipArchive::FL_NOCASE | Integer | Ignore case on name lookup |
ZipArchive::FL_NODIR | Integer | Ignore directory component |
ZipArchive::FL_COMPRESSED | Integer | Read compressed data |
ZipArchive::FL_UNCHANGED | Integer | Use original data, ignoring changes. |
ZipArchive::FL_RECOMPRESS | Integer | Force recompression of data. Available as of PHP 8.0.0 and PECL zip 1.18.0. |
ZipArchive::FL_ENCRYPTED | Integer | Read encrypted data (implies FL_COMPRESSED). Available as of PHP 8.0.0 and PECL zip 1.18.0. |
ZipArchive::FL_OVERWRITE | Integer | If file with name exists, overwrite (replace) it. Available as of PHP 8.0.0 and PECL zip 1.18.0. |
ZipArchive::FL_LOCAL | Integer | In local header. Available as of PHP 8.0.0 and PECL zip 1.18.0. |
ZipArchive::ZIP_FL_CENTRAL | Integer | In central directory. Available as of PHP 8.0.0 and PECL zip 1.18.0. |
ZipArchive::FL_ENC_GUESS | Integer | Guess string encoding (is default). Available as of PHP 7.0.8. |
ZipArchive::FL_ENC_RAW | Integer | Get unmodified string. Available as of PHP 7.0.8. |
ZipArchive::FL_ENC_STRICT | Integer | Follow specification strictly. Available as of PHP 7.0.8. |
ZipArchive::FL_ENC_UTF_8 | Integer | String is UTF-8 encoded. Available as of PHP 7.0.8. |
ZipArchive::FL_ENC_CP437 | Integer | String is CP437 encoded. Available as of PHP 7.0.8. |
ZipArchive::CM_DEFAULT | Integer | better of deflate or store. |
ZipArchive::CM_STORE | Integer | stored (uncompressed). |
ZipArchive::CM_SHRINK | Integer | shrunk |
ZipArchive::CM_REDUCE_1 | Integer | reduced with factor 1 |
ZipArchive::CM_REDUCE_2 | Integer | reduced with factor 2 |
ZipArchive::CM_REDUCE_3 | Integer | reduced with factor 3 |
ZipArchive::CM_REDUCE_4 | Integer | reduced with factor 4 |
ZipArchive::CM_IMPLODE | Integer | imploded |
ZipArchive::CM_DEFLATE | Integer | deflated |
ZipArchive::CM_DEFLATE64 | Integer | deflate64 |
ZipArchive::CM_PKWARE_IMPLODE | Integer | PKWARE imploding |
ZipArchive::CM_BZIP2 | Integer | BZIP2 algorithm |
ZipArchive::CM_LZMA | Integer | LZMA algorithm |
ZipArchive::CM_LZMA2 | Integer | LZMA2 algorithm. Available as of PHP 7.4.3 and PECL zip 1.16.0, respectively, if built against libzip ≥ 1.6.0. |
ZipArchive::CM_ZSTD | Integer | Zstandard algorithm. Available as of PHP 8.0.0 and PECL zip 1.19.1, respectively, if built against libzip ≥ 1.8.0. |
ZipArchive::CM_XZ | Integer | XZ algorithm. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.6.0. |
ZipArchive::ER_OK | Integer | No error. |
ZipArchive::ER_MULTIDISK | Integer | Multi-disk zip archives not supported. |
ZipArchive::ER_RENAME | Integer | Renaming temporary file failed. |
ZipArchive::ER_CLOSE | Integer | Closing zip archive failed |
ZipArchive::ER_SEEK | Integer | Seek error |
ZipArchive::ER_READ | Integer | Read error |
ZipArchive::ER_WRITE | Integer | Write error |
ZipArchive::ER_CRC | Integer | CRC error |
ZipArchive::ER_ZIPCLOSED | Integer | Containing zip archive was closed |
ZipArchive::ER_NOENT | Integer | No such file |
ZipArchive::ER_EXISTS | Integer | File already exists |
ZipArchive::ER_OPEN | Integer | Can't open file |
ZipArchive::ER_TMPOPEN | Integer | Failure to create temporary file |
ZipArchive::ER_ZLIB | Integer | Zlib error |
ZipArchive::ER_MEMORY | Integer | Memory allocation failure |
ZipArchive::ER_CHANGED | String | Entry has been changed |
ZipArchive::ER_COMPNOTSUPP | Integer | Compression method not supported |
ZipArchive::ER_EOF | Integer | Premature EOF |
ZipArchive::ER_INVAL | Integer | Invalid argument |
ZipArchive::ER_NOZIP | Integer | Not a zip archive |
ZipArchive::ER_INTERNAL | Integer | Internal error |
ZipArchive::ER_INCONS | Integer | Zip archive inconsistent |
ZipArchive::ER_REMOVE | Integer | Can't remove file |
ZipArchive::ER_DELETED | Integer | Entry has been deleted |
ZipArchive::ER_ENCRNOTSUPP | Integer | Encryption method not supported. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively. |
ZipArchive::ER_RDONLY | Integer | Read-only archive. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively. |
ZipArchive::ER_NOPASSWD | Integer | No password provided. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively. |
ZipArchive::ER_WRONGPASSWD | Integer | Wrong password provided. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively. |
ZipArchive::ZIP_ER_OPNOTSUPP | Integer | Operation not supported. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.0.0. |
ZipArchive::ZIP_ER_INUSE | Integer | Resource still in use. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.0.0. |
ZipArchive::ZIP_ER_TELL | Integer | Tell error. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.0.0. |
ZipArchive::ZIP_ER_COMPRESSED_DATA | Integer | Compressed data invalid. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.6.0. |
ZipArchive::ER_CANCELLED | Integer | Operation cancelled. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.6.0. |
ZipArchive::EM_NONE | Integer | No encryption. Available as of PHP 7.2.0 and PECL zip 1.14.0, respectively. |
ZipArchive::EM_TRAD_PKWARE | Integer | Traditional PKWARE encryption. Available as of PHP 8.0.0 and PECL zip 1.19.0, respectively. |
ZipArchive::EM_AES_128 | Integer | AES 128 encryption. Available as of PHP 7.2.0 and PECL zip 1.14.0, respectively, if built against libzip ≥ 1.2.0. |
ZipArchive::EM_AES_192 | Integer | AES 192 encryption. Available as of PHP 7.2.0 and PECL zip 1.14.0, respectively, if built against libzip ≥ 1.2.0. |
ZipArchive::EM_AES_256 | Integer | AES 256 encryption. Available as of PHP 7.2.0 and PECL zip 1.14.0, respectively, if built against libzip ≥ 1.2.0. |
ZipArchive::EM_UNKNOWN | Integer | Unknown encryption algorithm. Available as of PHP 8.0.0 and PECL zip 1.19.0, respectively. |
ZipArchive::LIBZIP_VERSION | Integer | Zip library version. Available as of PHP 7.4.3 and PECL zip 1.16.0. |
Operating system constants for external attributes.
- ZipArchive::OPSYS_DOS (int)
- ZipArchive::OPSYS_AMIGA (int)
- ZipArchive::OPSYS_OPENVMS (int)
- ZipArchive::OPSYS_UNIX (int)
- ZipArchive::OPSYS_VM_CMS (int)
- ZipArchive::OPSYS_ATARI_ST (int)
- ZipArchive::OPSYS_OS_2 (int)
- ZipArchive::OPSYS_MACINTOSH (int)
- ZipArchive::OPSYS_Z_SYSTEM (int)
- ZipArchive::OPSYS_CPM (int)
- ZipArchive::OPSYS_WINDOWS_NTFS (int)
- ZipArchive::OPSYS_MVS (int)
- ZipArchive::OPSYS_VSE (int)
- ZipArchive::OPSYS_ACORN_RISC (int)
- ZipArchive::OPSYS_VFAT (int)
- ZipArchive::OPSYS_ALTERNATE_MVS (int)
- ZipArchive::OPSYS_BEOS (int)
- ZipArchive::OPSYS_TANDEM (int)
- ZipArchive::OPSYS_OS_400 (int)
- ZipArchive::OPSYS_OS_X (int)
- ZipArchive::OPSYS_DEFAULT (int) - Since PECL zip 1.12.4