PHP | zip_entry_open() Function
Last Updated :
12 Apr, 2022
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
php
<?php
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip");
$zip_entry = zip_read( $zip_handle );
zip_entry_open( $zip_handle , $zip_entry , "rb");
$file = zip_entry_name( $zip_entry );
if ( $file == true)
echo ("Zip file: " . $file . " open successfully <br>");
$flag = zip_entry_close( $zip_entry );
if ( $flag == true)
echo ("Zip file: " . $file . " closed successfully");
else
echo ("Zip file: " . $file . " cannot be closed");
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
<?php
$zip_handle = zip_open("C:/xampp/htdocs/articles.zip");
if ( is_resource ( $zip_handle ))
{
while ( $zip_entry = zip_read( $zip_handle ))
{
$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>" ;
$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>");
}
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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...