When working with ZIP files in PHP, the zip_read function is a commonly used interface for reading file entries from the compressed archive one by one. However, sometimes we encounter unwanted folders or file entries, such as the __MACOSX directory generated by macOS or other hidden files. These entries are often irrelevant to our business logic and may even interfere with the file processing workflow. Therefore, it is necessary to skip them during iteration.
This article will introduce how to filter out these specific file entries when using the zip_read function.
Here is a simple example that demonstrates how to open a ZIP file and iterate through its file entries:
<?php
$zipPath = 'example.zip';
$zip = zip_open($zipPath);
if ($zip) {
while ($zipEntry = zip_read($zip)) {
$entryName = zip_entry_name($zipEntry);
echo "Found entry: $entryName\n";
}
zip_close($zip);
} else {
echo "Failed to open ZIP file.";
}
?>
We need to check the name of each file entry during iteration and exclude any folders starting with __MACOSX or other invalid entries.
The key here is to use PHP's string function strpos to detect the prefix of the entry name:
<?php
$zipPath = 'example.zip';
$zip = zip_open($zipPath);
if ($zip) {
while ($zipEntry = zip_read($zip)) {
$entryName = zip_entry_name($zipEntry);
// Skip all entries under the __MACOSX folder
if (strpos($entryName, '__MACOSX/') === 0) {
continue;
}
// You can also skip other unwanted hidden files, such as .DS_Store
if (basename($entryName) === '.DS_Store') {
continue;
}
// Process the file normally
echo "Processing file: $entryName\n";
// You can open the entry for reading, for example:
if (zip_entry_open($zip, $zipEntry)) {
$contents = zip_entry_read($zipEntry, zip_entry_filesize($zipEntry));
// Process $contents, e.g., save the file
zip_entry_close($zipEntry);
}
}
zip_close($zip);
} else {
echo "Unable to open ZIP file.";
}
?>
When using zip_read to iterate over ZIP entries, check the entry names.
Use strpos to determine if the entry name starts with __MACOSX/, and skip all files under that directory.
You can also filter based on specific file names, such as .DS_Store.
This approach effectively avoids processing unnecessary macOS system files, improving the robustness and efficiency of the code.