Current Location: Home> Latest Articles> How to Use zip_read to Check if the Last Zip Entry Has Been Read?

How to Use zip_read to Check if the Last Zip Entry Has Been Read?

M66 2025-06-23

When working with ZIP files in PHP, it's common to need to traverse all entries within a compressed archive. PHP provides the zip_read() function in combination with zip_entry_read() to read the contents of a ZIP file. However, a common issue is determining when the last entry has been read.

This article will explain in detail how to use PHP's ZIP extension and the zip_read() function to check if all entries in the ZIP archive have been read.

1. Basic Usage Review

First, we need to open a ZIP file using zip_open(), which returns a resource handle. Then, we use zip_read() to retrieve each entry object. The example below demonstrates this process:

$zip = zip_open("http://m66.net/path/to/yourfile.zip");  
if (is_resource($zip)) {  
    while ($entry = zip_read($zip)) {  
        echo "Reading entry: " . zip_entry_name($entry) . "\n";  
        // Read entry contents  
        if (zip_entry_open($zip, $entry)) {  
            $content = zip_entry_read($entry, zip_entry_filesize($entry));  
            echo $content;  
            zip_entry_close($entry);  
        }  
    }  
    zip_close($zip);  
} else {  
    echo "Failed to open ZIP file";  
}  

In the code above, we use while ($entry = zip_read($zip)) to sequentially retrieve each entry. The loop ends when all entries have been read.

2. How to Determine If the Last Entry Has Been Read?

The zip_read() function returns the next entry resource. If it returns false, this indicates that there are no more entries. Therefore, simply checking if zip_read() returns false is a way to determine if all entries have been read.

Example:

$zip = zip_open("http://m66.net/path/to/yourfile.zip");  
if (is_resource($zip)) {  
    while (($entry = zip_read($zip)) !== false) {  
        echo "Current entry: " . zip_entry_name($entry) . "\n";  
        // Process entry contents  
    }  
    echo "All entries have been read.\n";  
    zip_close($zip);  
}  

In this code, the while loop ends when zip_read() returns false, signaling that all entries have been traversed.

3. Complete Example

Here is an example that reads all entries of a ZIP file and outputs their names and contents:

<?php  
$zipFile = "http://m66.net/files/example.zip";  
<p>$zip = zip_open($zipFile);<br>
if (!is_resource($zip)) {<br>
die("Unable to open ZIP file: $zipFile\n");<br>
}</p>
<p>while (($entry = zip_read($zip)) !== false) {<br>
$entryName = zip_entry_name($entry);<br>
echo "Entry name: $entryName\n";</p>
    $entrySize = zip_entry_filesize($entry);  
    $content = zip_entry_read($entry, $entrySize);  
    echo "Content:\n$content\n";  
    zip_entry_close($entry);  
} else {  
    echo "Unable to open entry: $entryName\n";  
}  

}

echo "All entries have been read.\n";
zip_close($zip);
?>

4. Summary

  • zip_read() returns the next ZIP entry resource, and once all entries have been read, it returns false.

  • To check if all entries have been read, simply check if zip_read() returns false.

  • By combining zip_entry_open() and zip_entry_read(), you can read the content of each entry.

By following the methods outlined above, you can efficiently and accurately traverse all entries in a ZIP file and track the reading progress.