PHP Zip Functions

About This Index
This page is the PHP Zip Functions index for the tutorial track. Each row links to a full guide with syntax, examples, and FAQs for one procedural Zip function.
Learn how to create, list, read, and extract archives with ZipArchive on /php/zip. Use this page when you meet a legacy function such as zip_open(), zip_read(), or zip_entry_read() in older code.
Every function on this page is deprecated since PHP 8.0. Do not use them in new code. The ZipArchive class is the maintained API, it can also create and change archives, and each procedural function has a matching method. Read the PHP Zip page first.
The procedural API can only read archives. You open the archive to get a handle, call zip_read() to get an entry, and then open, read, and close that entry. There is no way to add files or save changes.
How to Call a PHP Zip Function
Every function is global—call it directly. The legacy loop below is what you will see in old scripts, followed by the ZipArchive version to use today:
// Legacy procedural API (deprecated since PHP 8.0)
$zip = zip_open('files.zip'); // resource|int
if (is_resource($zip)) {
while ($entry = zip_read($zip)) { // resource|false
echo zip_entry_name($entry), PHP_EOL; // string
echo zip_entry_filesize($entry), PHP_EOL; // int
echo zip_entry_compressedsize($entry), PHP_EOL; // int
echo zip_entry_compressionmethod($entry), PHP_EOL; // string, e.g. deflated
if (zip_entry_open($zip, $entry, 'r')) { // bool
$data = zip_entry_read($entry, zip_entry_filesize($entry)); // string|false
zip_entry_close($entry); // bool
}
}
zip_close($zip); // void
}
// Preferred: ZipArchive
$za = new ZipArchive();
if ($za->open('files.zip') === true) {
for ($i = 0; $i < $za->numFiles; $i++) {
$st = $za->statIndex($i); // name, size, comp_size, comp_method
$data = $za->getFromIndex($i); // string|false
}
$za->close();
} PHP Zip Functions Table
All function guides in this PHP tutorial series (10 tutorials in 3 categories). All of them are deprecated since PHP 8.0, so prefer ZipArchive.
Open & close archive
3 functionsOpen a ZIP file, step through its entries, and close it. These functions are deprecated since PHP 8.0; ZipArchive::open(), a for loop and ZipArchive::close() replace them.
| Function | Description | Tutorial |
|---|---|---|
zip_open() | Open a ZIP file for reading and return a handle. | Try it |
zip_close() | Close a ZIP archive handle. | Try it |
zip_read() | Read the next entry in the archive. | Try it |
Entry open, read & close
3 functionsOpen one entry, read its data in pieces, and close it. ZipArchive::getFromName() and getFromIndex() do all three steps in one call.
| Function | Description | Tutorial |
|---|---|---|
zip_entry_open() | Open an archive entry so its data can be read. | Try it |
zip_entry_read() | Read a number of bytes from an open entry. | Try it |
zip_entry_close() | Close an entry that was opened with zip_entry_open(). | Try it |
Entry info
4 functionsGet the name, sizes, and compression method of an entry. ZipArchive::statIndex() returns all of this in one array.
| Function | Description | Tutorial |
|---|---|---|
zip_entry_name() | Get the name of an archive entry. | Try it |
zip_entry_filesize() | Get the original (uncompressed) size of an entry. | Try it |
zip_entry_compressedsize() | Get the compressed size of an entry. | Try it |
zip_entry_compressionmethod() | Get the compression method used for an entry. | Try it |
Frequently Asked Questions
Start with zip_close()
Open the next guide—syntax, examples, and FAQs.
6 people found this page helpful
