zip_read()
The zip_read() function is a built-in function in PHP that is used to read the entries in a zip archive. The function requires an open file handle, which can be
⚠️ Legacy API Notice: The
zip_read()function and related procedural zip functions were removed in PHP 8.0. They require theext-zipPECL extension and are only available in PHP 7.4 and earlier. For modern PHP applications, theZipArchiveclass is recommended.
The zip_read() function is a built-in function in PHP that is used to read the entries in a zip archive. The function requires an open file handle, which can be obtained using zip_open(). Note that zip_read() returns false when no more entries are available, which naturally terminates iteration.
Syntax
The syntax of the zip_read() function is as follows:
Syntax of the zip_read() function in PHP
resource|false zip_read(resource $zip)Where $zip is the zip archive resource returned by zip_open().
Usage Examples
Let's take a look at a practical example of using zip_read() in PHP.
Example: Reading the Entries in a Zip Archive
Suppose you have opened a zip archive using the PHP zip functions and want to read the entries in the archive. You can use the zip_read() function to do this, like this:
Reading the Entries in a Zip Archive in PHP
$zip = zip_open("example.zip");
if ($zip === false) {
throw new RuntimeException("Failed to open zip archive");
}
while ($zip_entry = zip_read($zip)) {
echo "Name: " . zip_entry_name($zip_entry) . "\n";
echo "Compressed Size: " . zip_entry_compressedsize($zip_entry) . "\n";
echo "Uncompressed Size: " . zip_entry_filesize($zip_entry) . "\n";
}
zip_close($zip);This code opens a zip archive file example.zip using zip_open(). We then loop through the entries in the archive using zip_read() and output information about each entry using zip_entry_name(), zip_entry_compressedsize(), and zip_entry_filesize(). Finally, the zip archive resource is closed using zip_close().
Conclusion
In this article, we've discussed the PHP zip_read() function and how it can be used to read the entries in a zip archive. We've explained what the function does, its syntax, and provided an example of how it can be used in a practical scenario. By using zip_read() in your PHP applications, you can easily read and access the entries in a zip archive.
Practice
What is zip_read() function used for in PHP?