Skip to content

zip_entry_open()

⚠️ Deprecation Notice: The zip_entry_open() function belongs to the legacy zip extension, which was deprecated in PHP 7.4 and removed in PHP 8.0. The examples below are provided for historical reference only. For modern PHP applications, use the ZipArchive class instead.

The zip_entry_open() function is a built-in function in PHP that is used to open a file in a zip archive. The function returns a handle that can be used to read the contents of the file.

Syntax

The syntax of the zip_entry_open() function is as follows:

syntax of the zip_entry_open() function in PHP

php
resource zip_entry_open(resource $zip, resource $zip_entry [, string $mode = "r" [, resource $password ]])

Where $zip is the zip handle returned by zip_open(), $zip_entry is the zip_entry handle for the file in the zip archive, $mode is the mode in which to open the file, and $password is the password for encrypted zip archives.

Usage Examples

Let's take a look at a practical example of using zip_entry_open() in PHP.

Example: Opening a File in a Zip Archive

Suppose you have opened a zip archive using the PHP zip functions and want to open a file in the archive. You can use the zip_entry_open() function to do this, like this:

Opening a File in a Zip Archive in PHP

php
$zip = zip_open("example.zip");
$zip_entry = zip_read($zip);

// open the file
$zip_entry_handle = zip_entry_open($zip, $zip_entry, "r");

// read the contents of the file
$file_contents = zip_entry_read($zip_entry_handle, zip_entry_filesize($zip_entry));

// close the file handle
zip_entry_close($zip_entry_handle);

This code opens a zip archive file "example.zip" using zip_open(). We then read a file in the archive using zip_read() and open the file using zip_entry_open() with mode "r". The file contents are read using zip_entry_read() and the handle is closed using zip_entry_close().

Modern Alternative: ZipArchive Class

To achieve the same result in PHP 8.0+, use the built-in ZipArchive class:

php
$zip = new ZipArchive;
if ($zip->open('example.zip') === true) {
    // Get the first file entry in the archive
    $stat = $zip->statIndex(0);
    $file_contents = $zip->getFromName($stat['name']);
    $zip->close();
}

Conclusion

In this article, we've discussed the PHP zip_entry_open() function and how it can be used to open a file in a zip archive. We've explained what the function does, its syntax, and provided an example of how it can be used in a practical scenario. Note that zip_entry_open() is deprecated and removed in PHP 8.0; for modern projects, use the ZipArchive class to open and read files in a zip archive.

Practice

What does the ZipEntry::open() function in PHP do?

Dual-run preview — compare with live Symfony routes.