- PHP Zip
- An example to read(archieve) a zip file through php zip
- Output
- Requirements
- Installation
- Linux systems
- Windows
- Run-time Configuration
- Resource Types
- Predefined Constants
- Operating system constants for external attributes
- Zip Functions
- Zip Predefined Classes
- PHP Zip Extension
- Install ZIP Extensions in Ubuntu
- Use the ZipArchive() Class to Create a Zip File in PHP
- Use the ZipArchive() Class to Create a Zip File in PHP
- How to Use PHP Zip Extension to Get Information of All the Member Files of a ZIP
- Related Article — PHP Zip
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
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:
- ZipArchive::CREATE (integer)
- Create the archive if it does not exist.
- If archive exists, ignore its current contents. In other words, handle it the same way as an empty archive.
- Error if archive already exists.
- Perform additional consistency checks on the archive, and error if they fail.
- Ignore case on name lookup
- Ignore directory component
- Read compressed data
- Use original data, ignoring changes.
- Guess string encoding (is default). Available as of PHP 7.0.8.
- Get unmodified string. Available as of PHP 7.0.8.
- Follow specification strictly. Available as of PHP 7.0.8.
- String is UTF-8 encoded. Available as of PHP 7.0.8.
- String is CP437 encoded. Available as of PHP 7.0.8.
- better of deflate or store.
- stored (uncompressed).
- shrunk
- reduced with factor 1
- reduced with factor 2
- reduced with factor 3
- reduced with factor 4
- imploded
- deflated
- deflate64
- PKWARE imploding
- BZIP2 algorithm
- No error.
- Multi-disk zip archives not supported.
- Renaming temporary file failed.
- Closing zip archive failed
- Seek error
- Read error
- Write error
- CRC error
- Containing zip archive was closed
- No such file.
- File already exists
- Can’t open file
- Failure to create temporary file.
- Zlib error
- Memory allocation failure
- Entry has been changed
- Compression method not supported.
- Premature EOF
- Invalid argument
- Not a zip archive
- Internal error
- Zip archive inconsistent
- Can’t remove file
- Entry has been deleted
- No encryption, since PHP 7.2.0, PECL zip 1.14.0
- AES 128 encryption, since PHP 7.2.0, PECL zip 1.14.0
- AES 1192 encryption, since PHP 7.2.0, PECL zip 1.14.0
- AES 256 encryption, since PHP 7.2.0, PECL zip 1.14.0
Operating system constants for external attributes
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
PHP Zip Extension
- Install ZIP Extensions in Ubuntu
- Use the ZipArchive() Class to Create a Zip File in PHP
- Use the ZipArchive() Class to Create a Zip File in PHP
- How to Use PHP Zip Extension to Get Information of All the Member Files of a ZIP
Zip extension is one of the most important items in file handling. If you want to compress files or move multiple files through one file, ZIP will be required for this.
We need to install libzip to perform operations involving the ZIP extension for other platforms.
Install ZIP Extensions in Ubuntu
ZIP is already included in windows PHP. We will try to install this library for Ubuntu .
Run the following commands in Ubuntu to install the libzip library.
$ sudo apt-get update $ sudo apt-get install -y $ sudo apt-get install libzip-dev
Setting up libzip-dev:amd64 (1.7.3-1+ubuntu20.04.1+deb.sury.org+2) . Processing triggers for man-db (2.9.1-1) . Processing triggers for libc-bin (2.31-0ubuntu9.2) .
The next step is to install a PHP extension for ZIP .
$ sudo apt install php7.4-zip
Once it is successfully installed, you can use it in PHP.
Use the ZipArchive() Class to Create a Zip File in PHP
ZipArchive() is the class used to perform ZIP operations in PHP. Using the ZipArchive() class, create a Zip file.
php $create_zip = new ZipArchive(); $file_name = "./New.zip"; if ($create_zip->open($file_name, ZipArchive::CREATE)!==TRUE) exit("cannot open the zip file $file_name>\n"); > $current_dir=getcwd(); //Create files to add to the zip $create_zip->addFromString("file1 ". time().".txt" , "#1 This is This is the test file number one.\n"); $create_zip->addFromString("file2 ". time().".txt", "#2 This is This is the test file number one.\n"); //add files to the zip $create_zip->addFile($current_dir . "/too.php","/testfromfile.php"); echo "Number of files added: " . $create_zip->numFiles; echo "
"; echo "Failed to add:" . $create_zip->status ; $create_zip->close(); ?>The code above creates two text files with some content and adds them into a zip file.
Use the ZipArchive() Class to Create a Zip File in PHP
Let’s extract the zip file created in the first code using the ZipArchive() class of PHP.
php $extract_zip = new ZipArchive; $open_zip = $extract_zip->open('New.zip'); if ($open_zip === TRUE) $extract_to = getcwd(); $extract_zip->extractTo($extract_to); //extract to the current working directory. echo "Number of Files to be Extracted:" . $extract_zip->numFiles . "
"; $extract_zip->close(); echo 'Files Successfully Extracted!'; > else echo 'Cannot Extract!'; > ?>The above code will extract the New.zip file created in the first example.
How to Use PHP Zip Extension to Get Information of All the Member Files of a ZIP
The PHP ZIP extension can get the information of all the files inside a ZIP .
php $zip_file = zip_open("New.zip"); if ($zip_file) while ($zip_members = zip_read($zip_file)) echo "Name of the file: " . zip_entry_name($zip_members) . "
"; echo "Original Size of the File: " . zip_entry_filesize($zip_members) . "
"; echo "Compressed Size of the File: " . zip_entry_compressedsize($zip_members) . "
"; echo "Method of Compression: " . zip_entry_compressionmethod($zip_members) . "
"; if (zip_entry_open($zip_file, $zip_members, "r")) echo "Content of the file:
"; $buf = zip_entry_read($zip_members, zip_entry_filesize($zip_members)); echo "$buf
"; zip_entry_close($zip_members); > echo "
"; > zip_close($zip_file); > ?>The code above uses built-in ZIP functions to get the information of files inside a ZIP .
Name of the file: file1 1644842283.txt Original Size of the File: 45 Compressed Size of the File: 39 Method of Compression: deflated Content of the file: #1 This is test file number one. Name of the file: file2 1644842283.txt Original Size of the File: 45 Compressed Size of the File: 39 Method of Compression: deflated Content of the file: #2 This is test file number two.
Sheeraz is a Doctorate fellow in Computer Science at Northwestern Polytechnical University, Xian, China. He has 7 years of Software Development experience in AI, Web, Database, and Desktop technologies. He writes tutorials in Java, PHP, Python, GoLang, R, etc., to help beginners learn the field of Computer Science.
Related Article — PHP Zip