Example 1 — Close an Entry Stream
Read one entry through getStream(), then close the stream. The legacy calls appear as comments.
<?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
- Create the demo archive and open it with ZipArchive.
- Call getStream() to get a stream for hello. txt and read it.
- Close the stream with fclose() and print the result.
- Close the archive and delete the temp file.

