PHP Function Reference

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.
MethodsDescription
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

Note: The procedural API is deprecated as of PHP 8.0.0. ZipArchive should be used instead.

FunctionsDescription
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).

ConstantsTypeDescription
ZipArchive::CREATEIntegerCreate the archive if it does not exist.
ZipArchive::OVERWRITEIntegerIf archive exists, ignore its current contents. In other words, handle it the same way as an empty archive.
ZipArchive::EXCLIntegerError if archive already exists.
ZipArchive::RDONLYIntegerOpen 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::CHECKCONSIntegerPerform additional consistency checks on the archive, and error if they fail.
ZipArchive::FL_NOCASEIntegerIgnore case on name lookup
ZipArchive::FL_NODIRIntegerIgnore directory component
ZipArchive::FL_COMPRESSEDIntegerRead compressed data
ZipArchive::FL_UNCHANGEDIntegerUse original data, ignoring changes.
ZipArchive::FL_RECOMPRESSIntegerForce recompression of data. Available as of PHP 8.0.0 and PECL zip 1.18.0.
ZipArchive::FL_ENCRYPTEDIntegerRead encrypted data (implies FL_COMPRESSED). Available as of PHP 8.0.0 and PECL zip 1.18.0.
ZipArchive::FL_OVERWRITEIntegerIf file with name exists, overwrite (replace) it. Available as of PHP 8.0.0 and PECL zip 1.18.0.
ZipArchive::FL_LOCALIntegerIn local header. Available as of PHP 8.0.0 and PECL zip 1.18.0.
ZipArchive::ZIP_FL_CENTRALIntegerIn central directory. Available as of PHP 8.0.0 and PECL zip 1.18.0.
ZipArchive::FL_ENC_GUESSIntegerGuess string encoding (is default). Available as of PHP 7.0.8.
ZipArchive::FL_ENC_RAWIntegerGet unmodified string. Available as of PHP 7.0.8.
ZipArchive::FL_ENC_STRICTIntegerFollow specification strictly. Available as of PHP 7.0.8.
ZipArchive::FL_ENC_UTF_8IntegerString is UTF-8 encoded. Available as of PHP 7.0.8.
ZipArchive::FL_ENC_CP437IntegerString is CP437 encoded. Available as of PHP 7.0.8.
ZipArchive::CM_DEFAULTIntegerbetter of deflate or store.
ZipArchive::CM_STOREIntegerstored (uncompressed).
ZipArchive::CM_SHRINKIntegershrunk
ZipArchive::CM_REDUCE_1Integerreduced with factor 1
ZipArchive::CM_REDUCE_2Integerreduced with factor 2
ZipArchive::CM_REDUCE_3Integerreduced with factor 3
ZipArchive::CM_REDUCE_4Integerreduced with factor 4
ZipArchive::CM_IMPLODEIntegerimploded
ZipArchive::CM_DEFLATEIntegerdeflated
ZipArchive::CM_DEFLATE64Integerdeflate64
ZipArchive::CM_PKWARE_IMPLODEIntegerPKWARE imploding
ZipArchive::CM_BZIP2IntegerBZIP2 algorithm
ZipArchive::CM_LZMAIntegerLZMA algorithm
ZipArchive::CM_LZMA2IntegerLZMA2 algorithm. Available as of PHP 7.4.3 and PECL zip 1.16.0, respectively, if built against libzip ≥ 1.6.0.
ZipArchive::CM_ZSTDIntegerZstandard algorithm. Available as of PHP 8.0.0 and PECL zip 1.19.1, respectively, if built against libzip ≥ 1.8.0.
ZipArchive::CM_XZIntegerXZ algorithm. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.6.0.
ZipArchive::ER_OKIntegerNo error.
ZipArchive::ER_MULTIDISKIntegerMulti-disk zip archives not supported.
ZipArchive::ER_RENAMEIntegerRenaming temporary file failed.
ZipArchive::ER_CLOSEIntegerClosing zip archive failed
ZipArchive::ER_SEEKIntegerSeek error
ZipArchive::ER_READIntegerRead error
ZipArchive::ER_WRITEIntegerWrite error
ZipArchive::ER_CRCIntegerCRC error
ZipArchive::ER_ZIPCLOSEDIntegerContaining zip archive was closed
ZipArchive::ER_NOENTIntegerNo such file
ZipArchive::ER_EXISTSIntegerFile already exists
ZipArchive::ER_OPENIntegerCan't open file
ZipArchive::ER_TMPOPENIntegerFailure to create temporary file
ZipArchive::ER_ZLIBIntegerZlib error
ZipArchive::ER_MEMORYIntegerMemory allocation failure
ZipArchive::ER_CHANGEDStringEntry has been changed
ZipArchive::ER_COMPNOTSUPPIntegerCompression method not supported
ZipArchive::ER_EOFIntegerPremature EOF
ZipArchive::ER_INVALIntegerInvalid argument
ZipArchive::ER_NOZIPIntegerNot a zip archive
ZipArchive::ER_INTERNALIntegerInternal error
ZipArchive::ER_INCONSIntegerZip archive inconsistent
ZipArchive::ER_REMOVEIntegerCan't remove file
ZipArchive::ER_DELETEDIntegerEntry has been deleted
ZipArchive::ER_ENCRNOTSUPPIntegerEncryption method not supported. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively.
ZipArchive::ER_RDONLYIntegerRead-only archive. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively.
ZipArchive::ER_NOPASSWDIntegerNo password provided. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively.
ZipArchive::ER_WRONGPASSWDIntegerWrong password provided. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively.
ZipArchive::ZIP_ER_OPNOTSUPP IntegerOperation 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_INUSEIntegerResource 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_TELLIntegerTell 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_DATAIntegerCompressed 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_CANCELLEDIntegerOperation cancelled. Available as of PHP 7.4.3 and PECL zip 1.16.1, respectively, if built against libzip ≥ 1.6.0.
ZipArchive::EM_NONEIntegerNo encryption. Available as of PHP 7.2.0 and PECL zip 1.14.0, respectively.
ZipArchive::EM_TRAD_PKWAREIntegerTraditional PKWARE encryption. Available as of PHP 8.0.0 and PECL zip 1.19.0, respectively.
ZipArchive::EM_AES_128IntegerAES 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_192IntegerAES 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 IntegerAES 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 IntegerUnknown encryption algorithm. Available as of PHP 8.0.0 and PECL zip 1.19.0, respectively.
ZipArchive::LIBZIP_VERSIONIntegerZip 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