Current Location: Home> Latest Articles> Use zip_entry_read() to read the content of the current Zip entry

Use zip_entry_read() to read the content of the current Zip entry

M66 2025-06-06

When processing compressed files in PHP, zip_read() and zip_entry_read() are two very useful functions that can help us read the contents of Zip files. This article will explain in detail how to use these two functions in combination to read and output the content of each entry in the Zip compressed package item by item.


1. Function introduction

  • zip_read(resource $zip)
    This function accepts an open Zip resource and returns a resource pointing to a Zip entry, representing the current entry in Zip. If all entries are read, false is returned.

  • zip_entry_read(resource $zip_entry, int $len = 1024)
    This function reads the contents of the current Zip entry of the specified length. The resources and number of bytes to be read by the current Zip entry need to be passed in.


2. Sample code explanation

The following sample code shows how to open a Zip file, iterate through all entries, and output the content of each entry:

 <?php
// Open a Zip document,Path example:http://m66.net/example.zip
$zip = zip_open('http://m66.net/example.zip');

if (!$zip) {
    echo "Unable to open Zip document。";
    exit;
}

// Traversal Zip All entries in
while ($zip_entry = zip_read($zip)) {
    // Get the entry name
    $entry_name = zip_entry_name($zip_entry);
    echo "Read entries: $entry_name\n";

    // Open the current entry to read the content
    if (zip_entry_open($zip, $zip_entry)) {
        $content = '';
        
        // 循环Read entries内容,Avoid too large content to be read in one go
        while ($data = zip_entry_read($zip_entry, 1024)) {
            $content .= $data;
        }

        // Output entry content
        echo "The content is as follows:\n";
        echo $content . "\n";

        // Close the current entry
        zip_entry_close($zip_entry);
    } else {
        echo "Unable to open条目 $entry_name Perform read。\n";
    }

    echo "------------------------\n";
}

// closure Zip resource
zip_close($zip);
?>

3. Code description

  • The zip_open() function is used to open Zip files, supports local paths, or can be a remote URL (such as http://m66.net/example.zip in the example).

  • Read each entry through zip_read() loop.

  • Call zip_entry_open() on each entry to open it, and then use zip_entry_read() to read the content. Here, 1024 bytes are read at a time and loop until the content is finished.

  • After reading, close the entry and finally close the entire Zip file resource.


4. Things to note

  • PHP's zip_read function relies on the zip extension to ensure that the extension has been enabled in your environment.

  • Remote URL access requires the server to support allow_url_fopen .

  • For large files, it is recommended to reasonably control the read block size to avoid memory overflow.

  • This method is suitable for reading text files or smaller binary files. For complex Zip operations, you can consider using the ZipArchive class.