Handling compressed files in PHP is a common task, especially when dealing with uploaded archive data or automated deployments. Among the available methods, zip_read() is an older interface of ZipArchive that allows you to step through each entry within a ZIP file. Although this method has largely been replaced by the object-oriented ZipArchive class, zip_read() still has practical value for certain projects or compatibility requirements.
This article will demonstrate, through practical examples, how to use the zip_read() function to read each entry in a ZIP file step by step.
First, you need a ZIP file, which can be any collection of files, such as an archive containing several .txt files. Let's assume you already have a ZIP file named example.zip, and it is placed in a server directory, for example:
/var/www/html/files/example.zip
PHP provides the zip_open() function to open a ZIP file, which returns a resource handle. This handle will be used for subsequent entry reading.
<?php
$zipPath = '/var/www/html/files/example.zip';
$zip = zip_open($zipPath);
<p>if (is_resource($zip)) {<br>
echo "ZIP file opened successfully.\n";<br>
} else {<br>
echo "Unable to open ZIP file.\n";<br>
}<br>
?><br>
Once the ZIP file is open, we can use zip_read() to iterate through each entry in the file. Here is the complete reading process:
<?php
$zipPath = '/var/www/html/files/example.zip';
$zip = zip_open($zipPath);
<p>if (is_resource($zip)) {<br>
while ($zipEntry = zip_read($zip)) {<br>
$entryName = zip_entry_name($zipEntry);<br>
echo "Found entry: " . $entryName . "\n";</p>
$contents = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry));
echo "Content preview: " . substr($contents, 0, 100) . "\n\n";
zip_entry_close($zipEntry);
}
}
zip_close($zip);
} else {
echo "Failed to open ZIP file.\n";
}
?>
In this code, we retrieve the current entry using zip_read(), then get the filename using zip_entry_name() and read the file's content using zip_entry_read(). Note that since some entries may be binary files or large files, we only preview the first 100 characters here.
Let's assume you provide an upload interface, and the ZIP files uploaded by users are extracted and checked for validity. You can use zip_read() to iterate through the files and check if a specific file, such as config.json, exists:
<?php
$zipPath = '/var/www/html/uploads/user_upload.zip';
$zip = zip_open($zipPath);
$foundConfig = false;
<p>if (is_resource($zip)) {<br>
while ($zipEntry = zip_read($zip)) {<br>
if (zip_entry_name($zipEntry) === 'config.json') {<br>
$foundConfig = true;<br>
break;<br>
}<br>
}<br>
zip_close($zip);<br>
}</p>
<p>if ($foundConfig) {<br>
echo "The upload package contains config.json, which is valid.\n";<br>
} else {<br>
echo "The config.json file is missing, upload invalid.\n";<br>
}<br>
?><br>
zip_read() is a procedural, older interface, and it is recommended to use ZipArchive for new projects.
In high-concurrency scenarios, be especially careful with releasing file handles when manually operating on resource handles.
If using web-based file upload functionality, ensure the relevant upload size limits in php.ini are configured correctly, such as upload_max_filesize and post_max_size.
Although zip_read() is an older PHP ZIP operation function, it still has learning and practical value in certain scenarios. Through the examples in this article, you should now understand how to step through each entry in a ZIP file using zip_read(), and apply it in real projects, such as automated content validation or file upload checks.