PHP Zip
PHP extension enables you to transparently read or write ZIP compressed archives and the files inside them.
An example to read(archieve) a zip file through php zip
<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip');
if ($res === TRUE) {
echo 'ok';
$zip->extractTo('test');
$zip->close();
} else {
echo 'failed, code:' . $res;
}
?>
Output
Requirements
This extension requires » libzip which is bundled with PHP.
Installation
Linux systems
In order to use these functions you must compile PHP with zip support by using the --enable-zip configure option.
As of PHP 5.6.0 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
As of PHP 5.3 this extension is built-in. Before, Windows users need to enable php_zip.dll inside of php.ini in order to use these functions.
Run-time Configuration
This extension has no configuration directives defined in php.ini.
Resource Types
There are two resource types used in the Zip module. The first one is the Zip directory for the Zip archive, the second Zip Entry for the archive entries.
Predefined Constants
PHP zip also provided some predefined constants, In order to use this 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_)
- mode (no prefix)
- 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::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_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::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::EM_NONE (integer)
- No encryption, since PHP 7.2.0, PECL zip 1.14.0
- ZipArchive::EM_AES_128 (integer)
- AES 128 encryption, since PHP 7.2.0, PECL zip 1.14.0
- ZipArchive::EM_AES_192 (integer)
- AES 1192 encryption, since PHP 7.2.0, PECL zip 1.14.0
- ZipArchive::EM_AES_256 (integer)
- AES 256 encryption, since PHP 7.2.0, PECL zip 1.14.0
-
Operating system constants for external attributes
- ZipArchive::OPSYS_DOS (integer)
- ZipArchive::OPSYS_AMIGA (integer)
- ZipArchive::OPSYS_OPENVMS (integer)
- ZipArchive::OPSYS_UNIX (integer)
- ZipArchive::OPSYS_VM_CMS (integer)
- ZipArchive::OPSYS_ATARI_ST (integer)
- ZipArchive::OPSYS_OS_2 (integer)
- ZipArchive::OPSYS_MACINTOSH (integer)
- ZipArchive::OPSYS_Z_SYSTEM (integer)
- ZipArchive::OPSYS_CPM (integer)
- ZipArchive::OPSYS_WINDOWS_NTFS (integer)
- ZipArchive::OPSYS_MVS (integer)
- ZipArchive::OPSYS_VSE (integer)
- ZipArchive::OPSYS_ACORN_RISC (integer)
- ZipArchive::OPSYS_VFAT (integer)
- ZipArchive::OPSYS_ALTERNATE_MVS (integer)
- ZipArchive::OPSYS_BEOS (integer)
- ZipArchive::OPSYS_TANDEM (integer)
- ZipArchive::OPSYS_OS_400 (integer)
- ZipArchive::OPSYS_OS_X (integer)
- ZipArchive::OPSYS_DEFAULT (integer)
- Since PHP 5.6.0, PECL zip 1.12.4
Zip Functions
- zip_close — Close a ZIP file archive
- zip_entry_close — Close a directory entry
- zip_entry_compressedsize — Retrieve the compressed size of a directory entry
- zip_entry_compressionmethod — Retrieve the compression method of a directory entry
- zip_entry_filesize — Retrieve the actual file size of a directory entry
- zip_entry_name — Retrieve the name of a directory entry
- zip_entry_open — Open a directory entry for reading
- zip_entry_read — Read from an open directory entry
- zip_open — Open a ZIP file archive
- zip_read — Read next entry in a ZIP file archive
Zip Predefined Classes
ZipArchive: This is a file archive, compressed with Zip.
- ZipArchive::addEmptyDir — Add a new directory
- ZipArchive::addFile — Adds a file to a ZIP archive from the given path
- ZipArchive::addFromString — Add a file to a ZIP archive using its contents
- ZipArchive::addGlob — Add files from a directory by glob pattern
- ZipArchive::addPattern — Add files from a directory by PCRE pattern
- ZipArchive::close — Close the active archive (opened or newly created)
- ZipArchive::count — Counts the number of files in the achive
- ZipArchive::deleteIndex — Delete an entry in the archive using its index
- ZipArchive::deleteName — Delete an entry in the archive using its name
- ZipArchive::extractTo — Extract the archive contents
- ZipArchive::getArchiveComment — Returns the Zip archive comment
- ZipArchive::getCommentIndex — Returns the comment of an entry using the entry index
- ZipArchive::getCommentName — Returns the comment of an entry using the entry name
- ZipArchive::getExternalAttributesIndex — Retrieve the external attributes of an entry defined by its index
- ZipArchive::getExternalAttributesName — Retrieve the external attributes of an entry defined by its name
- ZipArchive::getFromIndex — Returns the entry contents using its index
- ZipArchive::getFromName — Returns the entry contents using its name
- ZipArchive::getNameIndex — Returns the name of an entry using its index
- ZipArchive::getStatusString — Returns the status error message, system and/or zip messages
- ZipArchive::getStream — Get a file handler to the entry defined by its name (read only)
- ZipArchive::locateName — Returns the index of the entry in the archive
- ZipArchive::open — Open a ZIP file archive
- ZipArchive::renameIndex — Renames an entry defined by its index
- ZipArchive::renameName — Renames an entry defined by its name
- ZipArchive::setArchiveComment — Set the comment of a ZIP archive
- ZipArchive::setCommentIndex — Set the comment of an entry defined by its index
- ZipArchive::setCommentName — Set the comment of an entry defined by its name
- ZipArchive::setCompressionIndex — Set the compression method of an entry defined by its index
- ZipArchive::setCompressionName — Set the compression method of an entry defined by its name
- ZipArchive::setEncryptionIndex — Set the encryption method of an entry defined by its index
- ZipArchive::setEncryptionName — Set the encryption method of an entry defined by its name
- ZipArchive::setExternalAttributesIndex — Set the external attributes of an entry defined by its index
- ZipArchive::setExternalAttributesName — Set the external attributes of an entry defined by its name
- ZipArchive::setPassword — Set the password for the active archive
- ZipArchive::statIndex — Get the details of an entry defined by its index
- ZipArchive::statName — Get the details of an entry defined by its name
- ZipArchive::unchangeAll — Undo all changes done in the archive
- ZipArchive::unchangeArchive — Revert all global changes done in the archive
- ZipArchive::unchangeIndex — Revert all changes done to an entry at the given index
- ZipArchive::unchangeName — Revert all changes done to an entry with the given name