Open In App

PHP | zip_entry_open() Function

The zip_entry_open() function is an inbuilt function in PHP which is used to open a zip entry archive for reading. Opening a file or a directory in a zip archive with the zip_entry_open function creates a new stream and establishes a connection between the stream and a file or a directory in a Zip Archive. The zip resource and the zip entry resource to be opened and sent as parameters to the zip_entry_open() function and it returns True on success and False on failure. 

Syntax:



bool zip_entry_open( $zip, $zip_entry, $mode )

Parameters: This function accepts three parameters as mentioned above and described below:

Return Value: It returns True on success or False on failure. 



Errors And Exceptions:

Below programs illustrate the zip_entry_open() function in PHP: 

Program 1:

Suppose a zip file articles.zip contains the following file: geeks.txt




<?php
 
// Opening a zip file
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip");
$zip_entry = zip_read($zip_handle);
 
// Opening a zip entry archive
zip_entry_open($zip_handle, $zip_entry, "rb");
$file = zip_entry_name($zip_entry);
 
if($file == true)
    echo("Zip file: " . $file . " open successfully <br>");
// Closing a zip entry archive
$flag = zip_entry_close($zip_entry);
if ($flag == true)
    echo("Zip file: " . $file . " closed successfully");
else
    echo("Zip file: " . $file . " cannot be closed");
 
// Closing zip file
zip_close($zip_handle);
?>

Output:

Zip file: articles/geeks open successfully 
Zip file: articles/geeks closed successfully

Program 2:

Suppose a zip file articles.zip contains the following files: geeks.txt geeks1.txt




<?php
 
// Opening a zip file
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip");
 
if(is_resource($zip_handle))
{
    while($zip_entry = zip_read($zip_handle))
    {
   
        // Opening a zip archive entry
        $file = zip_entry_open($zip_handle, $zip_entry, "rb");
        $file_name = zip_entry_name($zip_entry);
         
        if ($file == true)
        {
            echo("Zip file: " . $file_name . " open successfully");
            echo "<br>" ;
   
            // Closing a zip archive entry
            $flag = zip_entry_close($zip_entry);
             
            if ($flag == true)
                  echo("Zip file: " . $file_name .
                      " closed successfully <br><br>");
            else
                echo("Zip file: " . $file_name .
                         " cannot be closed <br><br>");
        }
        else
            echo("Zip Entry Cannot be opened.<br>");
    }
   
    // Closing a zip archive
    zip_close($zip_handle);
}
else
    echo("Failed to Open" . $zip_handle );
?>

Output:

Zip file: articles/geeks open successfully
Zip file: articles/geeks closed successfully 

Zip file: articles/geeks1 open successfully
Zip file: articles/geeks1 closed successfully

Related Articles:

Reference: http://php.net/manual/en/function.zip-entry-open.php


Article Tags :