PHP zip_entry_close() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

Definition

In PHP, zip_entry_close() closes a single entry that was opened with the old procedural zip_entry_open() function.

It is deprecated as of PHP 8.0. With ZipArchive you read an entry through getFromName() (nothing to close) or getStream() and then close the stream with fclose().

Syntax

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

PHP
// Deprecated (PHP 8.0+)
zip_entry_open($zip, $entry);
zip_entry_close($entry);

// Modern equivalent
$fp = $zip->getStream('hello.txt');
fclose($fp);
CallWhat it does
zip_entry_close($entry)Deprecated: closes the entry handle after you finish reading it.
@zip_entry_close($entry)Deprecated: the @ operator hides the E_DEPRECATED notice only.
fclose($zip->getStream('hello.txt'))Modern ZipArchive: closes a stream opened with getStream(). getFromName() needs no close.

zip_entry_close() is deprecated in PHP 8.0. ZipArchive has no entry handle object: use getFromName() for a quick read, or getStream() plus fclose() when you want to read in pieces.

Library declarations
PHP
zip_entry_close(resource $zip_entry): bool

Parameters

ParameterDescription
zip_entryRequired. A directory entry resource returned by zip_read() and opened with zip_entry_open().

Return Value

Return ValueDescription
trueThe entry was closed successfully.
falseThe entry could not be closed.

PHP Examples

Example 1 — Close an Entry Stream

Read one entry through getStream(), then close the stream. The legacy calls appear as comments.

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):
// zip_entry_open($zip, $entry, 'rb');
// $text = zip_entry_read($entry, 1024);
// zip_entry_close($entry);

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

$fp = $zip->getStream('hello.txt');
$text = stream_get_contents($fp);
echo 'Content: ', trim($text), PHP_EOL;
echo 'Closed: ', fclose($fp) ? 'yes' : 'no', PHP_EOL;

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

How It Works

  1. Create the demo archive and open it with ZipArchive.
  2. Call getStream() to get a stream for hello. txt and read it.
  3. Close the stream with fclose() and print the result.
  4. Close the archive and delete the temp file.

Example 2 — A Closed Stream Is No Longer a Resource

After fclose(), the handle cannot be used again. Check it with is_resource().

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);

$fp = $zip->getStream('docs/readme.txt');
echo 'Before close: ', is_resource($fp) ? 'resource' : 'not a resource', PHP_EOL;
fclose($fp);
echo 'After close: ', is_resource($fp) ? 'resource' : 'not a resource', PHP_EOL;

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

How It Works

  1. Open the demo archive and get a stream for docs/readme. txt.
  2. Use is_resource() to confirm the stream is valid.
  3. Close it with fclose() and test is_resource() again.
  4. Close the archive and remove the temp file.

Example 3 — Open and Close Every Entry

Loop through all entries, read each one, and close its stream before moving on.

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);

for ($i = 0; $i < $zip->numFiles; $i++) {
    $name = $zip->getNameIndex($i);
    $fp = $zip->getStream($name);
    $data = stream_get_contents($fp);
    fclose($fp);
    echo $name, ': ', strlen($data), ' bytes', PHP_EOL;
}

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

How It Works

  1. Open the demo archive that holds two text files.
  2. Loop over each index and look up the entry name.
  3. Read the stream, then close it with fclose().
  4. Print the name and byte count, then clean up the archive.

Frequently Asked Questions

Yes. It is part of the procedural zip_* API that is deprecated as of PHP 8.0. Use ZipArchive instead.
If you used getStream(), close that stream with fclose(). If you used getFromName() or getFromIndex(), there is nothing to close because they return a string.
Yes, it is good practice. Finish with each entry or stream first, then call close() on the archive.
No. It only closes that one stream. Call $zip->close() separately for the archive itself.
The entry name was not found or could not be opened. Check for false before calling fread() or fclose().

Next: zip_entry_compressedsize()

zip_entry_compressedsize() returns how many bytes a ZIP entry takes up inside the archive .

zip_entry_compressedsize() 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