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: neveropen.txt
php
<?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/neveropen open successfully Zip file: articles/neveropen closed successfully
Program 2:
Suppose a zip file articles.zip contains the following files: neveropen.txt neveropen1.txt
php
<?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/neveropen open successfully Zip file: articles/neveropen closed successfully Zip file: articles/neveropen1 open successfully Zip file: articles/neveropen1 closed successfully
Related Articles:
- PHP | zip_entry_close() Function
- PHP | zip_entry_compressedsize() Function
- PHP | zip_entry_name() Function
- PHP | zip_entry_filesize() Function
Reference: http://php.net/manual/en/function.zip-entry-open.php