zip_entry_read() function in PHP

The zip_entry_read() function is used to get the contents from an open zip archive file entry in PHP.

Syntax

zip_entry_read(zip_entry, len)

Parameters

  • zip_entry − The zip entry resource. Required.

  • len − The length in bytes to read. Optional, defaults to 1024.

Return Value

Returns the contents from an open zip archive file entry as a string. Returns FALSE on failure.

Example

Let's say we have a zip file "one.zip" containing a single file "detail.txt" with the following content −

Asia is a continent!

Here's how to read the contents using zip_entry_read()

<?php
    $zip_file = zip_open("one.zip");
    if ($zip_file) {
        while ($zip_entry = zip_read($zip_file)) {
            if (zip_entry_open($zip_file, $zip_entry)) {
                echo "File name: " . zip_entry_name($zip_entry) . "<br/>";
                echo "Content: " . zip_entry_read($zip_entry) . "<br/>";
                zip_entry_close($zip_entry);
            }
        }
        zip_close($zip_file);
    }
?>

Output

File name: detail.txt
Content: Asia is a continent!

Reading with Length Parameter

You can specify the number of bytes to read using the second parameter −

<?php
    $zip_file = zip_open("one.zip");
    if ($zip_file) {
        while ($zip_entry = zip_read($zip_file)) {
            if (zip_entry_open($zip_file, $zip_entry)) {
                // Read only first 10 bytes
                $content = zip_entry_read($zip_entry, 10);
                echo "First 10 bytes: " . $content . "<br/>";
                zip_entry_close($zip_entry);
            }
        }
        zip_close($zip_file);
    }
?>

Output

First 10 bytes: Asia is a 

Conclusion

The zip_entry_read() function allows you to extract content from zip file entries. Use the optional length parameter to control how many bytes to read at once.

Updated on: 2026-03-15T07:31:04+05:30

150 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements