W3docs

zip_entry_read()

The zip_entry_read() function is a built-in function in PHP that is used to read the contents of a file in a zip archive. The function requires an open file

The zip_entry_read() function was a built-in PHP function used to read the contents of a single file (an entry) inside an open zip archive, a chunk of bytes at a time. It returned the read data as a string.

Important: The entire procedural zip_entry_* family — including zip_entry_read() — was deprecated in PHP 7.4 and removed in PHP 8.0. There is no drop-in replacement; instead, modern PHP reads zip entries through the object-oriented ZipArchive class. This page documents the legacy function for reference and then shows the ZipArchive approach you should use today. To work with zip files at all, the zip extension must be enabled in your PHP build.

Syntax (Legacy)

string zip_entry_read(resource $zip_entry, int $length = 1024)
ParameterDescription
$zip_entryThe entry handle returned by zip_entry_open() after the archive was opened with zip_open() and an entry obtained from zip_read().
$lengthOptional. The number of bytes to read. Because zip entries are stored compressed, this is the number of uncompressed bytes returned. Defaults to 1024.

Return value: the read contents as a string, or false on failure. Like reading from a file, repeated calls advance through the entry until it is exhausted.

Reading a zip entry with ZipArchive (modern)

The cleanest replacement is ZipArchive::getFromName(), which returns the entire decompressed contents of an entry by its filename — no manual open/read/close loop per entry.

Reading one file from a zip archive in PHP

$zip = new ZipArchive();

if ($zip->open('example.zip') === true) {
    $contents = $zip->getFromName('readme.txt');
    $zip->close();

    if ($contents !== false) {
        echo $contents;
    } else {
        echo "Entry not found in archive.";
    }
} else {
    echo "Failed to open archive.";
}

Here open() returns true on success (it can also return an error code, so compare strictly with === true). getFromName() decompresses and returns the whole entry in one call. Use getFromIndex($i) instead when you want the entry at a numeric position rather than by name.

Looping over every entry

A common real task is to read all files in an archive. numFiles gives the entry count and statIndex() gives each entry's metadata:

$zip = new ZipArchive();

if ($zip->open('example.zip') === true) {
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $name = $zip->getNameIndex($i);
        $data = $zip->getFromIndex($i);
        echo $name . " (" . strlen($data) . " bytes)\n";
    }
    $zip->close();
}

Streaming large entries

getFromName() loads the whole entry into memory. For a large file, open the entry as a stream and read it in chunks instead — this is the true modern equivalent of the chunked zip_entry_read() behaviour:

$zip = new ZipArchive();

if ($zip->open('example.zip') === true) {
    $stream = $zip->getStream('big-log.txt');
    if ($stream) {
        while (!feof($stream)) {
            $chunk = fread($stream, 8192); // read 8 KB at a time
            echo $chunk;
        }
        fclose($stream);
    }
    $zip->close();
}

The returned stream is an ordinary PHP stream resource, so familiar functions like fread() and feof() work on it directly.

When would I use this?

  • Reading config or template files shipped inside a .zip without extracting them to disk.
  • Inspecting an upload (for example, a .docx or .xlsx, which are zip containers) entry by entry.
  • Processing large archived logs by streaming instead of unzipping everything first.

If you only need the bytes of a single file and don't care that it lives in a zip, extracting it first and using file_get_contents() is also a valid option.

Conclusion

zip_entry_read() belonged to PHP's legacy procedural zip API, which was deprecated in 7.4 and removed in 8.0. On any supported PHP version, read zip entries with the ZipArchive class: use getFromName() / getFromIndex() for whole entries, loop with numFiles, and use getStream() with fread() when an entry is too large to hold in memory at once.

Practice

Practice
On PHP 8.0+, which is the correct way to read a single file's contents from an open ZipArchive?
On PHP 8.0+, which is the correct way to read a single file's contents from an open ZipArchive?
Was this page helpful?