PHP zip_entry_read() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

Definition

In PHP, zip_entry_read() reads up to a given number of bytes from an opened ZIP entry.

It is deprecated as of PHP 8.0. With ZipArchive, use getFromName() or getFromIndex() for a whole entry, or getStream() with fread() to read it in chunks.

Syntax

Call zip_entry_read() with the values you need. Start with this line.

PHP
// Deprecated (PHP 8.0+)
$data = zip_entry_read($entry, 1024);

// Modern equivalent
$data = $zip->getFromName('hello.txt');
CallWhat it does
zip_entry_read($entry)Deprecated: reads up to 1024 bytes (the default) from the opened entry.
zip_entry_read($entry, zip_entry_filesize($entry))Deprecated: reads the whole entry by asking for its full size.
$zip->getFromName('hello.txt', 5)Modern ZipArchive: reads only the first 5 bytes. Leave the length out to read everything.

zip_entry_read() is deprecated in PHP 8.0. Remember that it read only $len bytes at a time, so large files needed a loop. ZipArchive::getFromName() returns everything at once when you omit the length.

Library declarations
PHP
zip_entry_read(resource $zip_entry, int $len = 1024): string|false

Parameters

ParameterDescription
zip_entryRequired. A directory entry resource that was opened with zip_entry_open().
lenOptional. How many bytes to read. The default is 1024.

Return Value

Return ValueDescription
stringThe data that was read. It can be shorter than len near the end of the entry.
falseThe data could not be read.

PHP Examples

Example 1 — Read a Whole Entry

Read hello.txt into a string with ZipArchive. The old zip_entry_read() call is shown as a comment.

PHP
<?php
function makeDemoZip(): string
{
    $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
    $zip = new ZipArchive();
    if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
        die("Cannot create zip\n");
    }
    $zip->addFromString('hello.txt', "Hello zip\n");
    $zip->addFromString('docs/readme.txt', "Read me\n");
    $zip->close();
    return $path;
}

// Legacy (deprecated since PHP 8.0):
// $text = zip_entry_read($entry, 1024);

$path = makeDemoZip();
$zip = new ZipArchive();
$zip->open($path);

$text = $zip->getFromName('hello.txt');
echo trim($text), PHP_EOL;

$zip->close();
unlink($path);

How It Works

  1. Create the demo archive and open it.
  2. Call getFromName() without a length to read the whole entry.
  3. Trim the trailing newline.
  4. Print the text, then close and delete the archive.

Example 2 — Read Only the First Bytes

Pass a length to getFromName() to read just the start of an entry, like a small zip_entry_read() length.

PHP
<?php
function makeDemoZip(): string
{
    $path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
    $zip = new ZipArchive();
    if ($zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE) !== true) {
        die("Cannot create zip\n");
    }
    $zip->addFromString('hello.txt', "Hello zip\n");
    $zip->addFromString('docs/readme.txt', "Read me\n");
    $zip->close();
    return $path;
}

$path = makeDemoZip();
$zip = new ZipArchive();
$zip->open($path);

echo 'First 5 bytes: ', $zip->getFromName('hello.txt', 5), PHP_EOL;
echo 'First 3 bytes: ', $zip->getFromName('docs/readme.txt', 3), PHP_EOL;

$zip->close();
unlink($path);

How It Works

  1. Create and open the demo archive.
  2. Read five bytes from hello. txt using the length argument.
  3. Read three bytes from docs/readme. txt the same way.
  4. Print both pieces, then close and delete the archive.

Example 3 — Read in Chunks With getStream()

Use getStream() and fread() to read a ten-byte entry four bytes at a time, like calling zip_entry_read() in a loop.

PHP
<?php
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('data.txt', 'ABCDEFGHIJ');
$zip->close();

$zip = new ZipArchive();
$zip->open($path);
$fp = $zip->getStream('data.txt');

$n = 1;
while (!feof($fp)) {
    $chunk = fread($fp, 4);
    if ($chunk === '' || $chunk === false) {
        break;
    }
    echo 'chunk ', $n, ': ', $chunk, PHP_EOL;
    $n++;
}

fclose($fp);
$zip->close();
unlink($path);

How It Works

  1. Create an archive with data. txt holding ten letters.
  2. Open it and get a stream with getStream().
  3. Loop with fread() and a length of four until the end of the stream.
  4. Print each chunk, then close the stream and archive and delete the file.

Frequently Asked Questions

Yes. It belongs to the procedural zip_* API that is deprecated as of PHP 8.0. Use ZipArchive::getFromName(), getFromIndex(), or getStream().
It reads at most len bytes (1024 by default). Pass the full size from zip_entry_filesize(), or in ZipArchive use getFromName() which reads it all.
Use getStream() and call fread() in a loop with a small chunk size. Close the stream with fclose() when you are done.
It returns false. Compare with === false so an empty entry (an empty string) is not mistaken for an error.
Both are just PHP strings of bytes. Use the right tools after reading, such as mb_* functions for text or file_put_contents() for binary files.

Next: zip_open()

zip_open() opens a ZIP archive file for reading and returns a resource you can pass to zip.

zip_open() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful