Current Location: Home> Latest Articles> How to Use zip_read in Conjunction with zip_open to Iterate and Read Each Entry in a Zip File?

How to Use zip_read in Conjunction with zip_open to Iterate and Read Each Entry in a Zip File?

M66 2025-06-23

In PHP, handling Zip archives is a very common task, especially when dealing with user-uploaded compressed files or bundled data files. ZipArchive is the recommended class for working with Zip files in modern PHP, but in certain scenarios or older PHP versions, zip_open and zip_read are still widely used. This article will explain how to use zip_open together with zip_read to iterate and read every entry in a Zip file.

Introduction to Basic Functions

PHP provides a set of low-level functions for working with Zip files, including:

  • zip_open($filename): Opens a Zip file and returns a Zip handle.

  • zip_read($zip): Reads the next entry, returning a resource for the entry.

  • zip_entry_name($entry): Retrieves the name of the entry.

  • zip_entry_open($zip, $entry): Opens an entry.

  • zip_entry_read($entry, $length): Reads the content of an opened entry.

  • zip_entry_close($entry): Closes the currently opened entry.

These functions allow us to access each file inside the archive in a streaming manner.

Sample Code

Below is a complete example demonstrating how to use these functions to read all entry contents from a Zip file:

$zipFile = '/path/to/your/archive.zip';

$zip = zip_open($zipFile);

if (is_resource($zip)) {
while ($entry = zip_read($zip)) {
$entryName = zip_entry_name($entry);
echo "Reading entry: " . $entryName . PHP_EOL;

    if (zip_entry_open($zip, $entry, "r")) {
        $content = '';
        while ($buffer = zip_entry_read($entry, 1024)) {
            $content .= $buffer;
        }
    echo $content . PHP_EOL;

    zip_entry_close($entry);
} else {
    echo "Unable to open entry: " . $entryName . PHP_EOL;
}

}
zip_close($zip);

} else {
echo "Unable to open Zip file." . PHP_EOL;
}

Use Cases

  • User-uploaded Zip files processing: For example, if a user uploads photos or text files as a Zip, we can use the above method to read and save each file to the server.

  • Batch configuration file reading: Some automation deployment systems package multiple configuration files into a Zip. By iterating through it, necessary information can be extracted.

  • Data migration and backup restoration: Read each data item from a backup Zip file and write it into the target system.

Important Notes

  1. zip_open and related functions are provided by PHP’s ZIP extension, so ensure that the ZIP extension is enabled in your PHP environment.

  2. These functions are suitable for simple Zip file handling. For encrypted archives or more complex structures, it’s recommended to use ZipArchive.

  3. zip_entry_read should typically be set to a maximum reading length of 1024 or another suitable value to avoid excessive memory consumption.

  4. When handling user-uploaded files, be cautious with file paths and contents to avoid Zip Slip attacks.

Practical Example

Let’s assume a Zip file is uploaded from the front-end to the server at the following address:

https://m66.net/uploads/data.zip

You can save it locally and then process it using the code from this article, categorizing or storing the contents of the Zip file.

Conclusion

By using zip_open and zip_read, PHP provides a low-level yet flexible way to iterate through entries in a Zip file. While ZipArchive is more recommended for modern development, these basic functions still hold practical value in certain environments. Understanding their inner workings gives you better control and flexibility when handling compressed files.