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:



  • $zip: It is a mandatory parameter which specifies the zip resource to be read.
  • $zip_entry: It is a mandatory parameter which specifies the zip entry resource.
  • $mode: It is an optional parameter which the access type of the required for the zip archive.

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

Errors And Exceptions:

  • The zip_entry_open() function returns an ER_OPEN error if the zip archive is invalid.
  • The zip_entry_open() function returns an ER_NOZIP error if the zip archive is empty.

Below programs illustrate the zip_entry_open() function in PHP:

Program 1:

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

filter_none

edit
close

play_arrow

link
brightness_4
code

<?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");
  
// Closeing zip file
zip_close($zip_handle);
?>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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



My Personal Notes arrow_drop_up

I am a technology enthusiast who has a keen interest in programming I am pursuing Engineering in Computer Science from GEU, Dehradun I like to unwind by watching movies and English sitcomsI have a keen interest in music

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.