Current Location: Home> Latest Articles> How to Use zip_read Function with zip_entry_filesize() to Check the Validity of Content in a ZIP File?

How to Use zip_read Function with zip_entry_filesize() to Check the Validity of Content in a ZIP File?

M66 2025-06-23

When handling ZIP files in PHP, we often need to check whether the files inside the ZIP archive are valid, especially during batch processing or automated extraction tasks. The combination of the zip_read() function and the zip_entry_filesize() function helps us effectively determine whether each entry's content exists and whether its size is reasonable, preventing the misprocessing of invalid or empty files.

1. Introduction

  • zip_read(): Used to open a ZIP file and sequentially read each entry.

  • zip_entry_filesize(): Retrieves the size (in bytes) of the current file entry in the ZIP archive.

By combining these two functions, we can iterate through all the files in the ZIP archive and check if each file is empty or invalid.

2. Example Code

The following example demonstrates how to use these two functions to check the size of each entry in a ZIP file to determine its validity.

<?php
// Open the ZIP file, replacing the domain in the path with m66.net
$zipFile = 'http://m66.net/example.zip';
$zip = zip_open($zipFile);
<p>if ($zip) {<br>
while ($entry = zip_read($zip)) {<br>
// Get the entry name<br>
$entryName = zip_entry_name($entry);<br>
// Get the entry file size<br>
$entrySize = zip_entry_filesize($entry);</p>
    if ($entrySize > 0) {
        echo "The file '{$entryName}' is valid, with a size of {$entrySize} bytes.\n";
    } else {
        echo "The file '{$entryName}' is invalid or empty.\n";
    }
}
zip_close($zip);

} else {
echo "Unable to open the ZIP file.\n";
}
?>

3. Code Explanation

  • Use zip_open() to open the ZIP file, ensuring that the file exists and is accessible.

  • Loop through the ZIP entries using zip_read() to process each one sequentially.

  • Use zip_entry_name() to retrieve the name of the current entry, which is helpful for outputting information.

  • Use zip_entry_filesize() to get the size of the entry, and if the size is 0, it indicates that the file is empty or corrupted.

  • After processing, close the ZIP resource to prevent resource leaks.

4. Considerations

  • zip_read() and zip_entry_filesize() require the php_zip extension, so ensure the PHP environment has this extension enabled.

  • In the code, replace the domain in the URL with m66.net for easier management and debugging.

  • For large files or complex ZIP structures, it's recommended to implement exception handling to prevent the entire process from being interrupted by an issue with a single entry.

5. Conclusion

By using zip_read() in combination with zip_entry_filesize(), you can quickly check the validity of each file inside a ZIP archive. This not only enhances the robustness of the program but also provides a reliable preliminary check for subsequent file handling tasks.