PHP | zip_entry_read() Function
The zip_entry_read() function is an inbuilt function in PHP which is used to read the contents from an opened zip archive entry. The zip entry is being read and the number of bytes to be returned can be sent as a parameter to the zip_entry_read() function and it returns the content of the specified zip entry on Success otherwise it returns a PHP warning.
Syntax:
string zip_entry_read( $zip_entry, $length )
Parameters: This function accept two parameters as mentioned above and described below.
- $zip_entry: It is a mandatory parameter which specifies the zip entry resource.
- $length: It is an optional parameter which specifies the number of bytes to be returned.
Return Value: It returns the content of specified zip entry on Success otherwise returns a PHP warning.
Errors And Exceptions:
- The zip_entry_read() function returns an ER_OPEN error if the zip archive is invalid.
- The zip_entry_read() function returns an ER_NOZIP error if the zip archive is empty.
Below programs illustrate the zip_entry_read() function in PHP:
Program 1:
Suppose a zip file article.zip contains the following file:
geeks.txt
<?php // Opening a zip file $zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); // Reading a zip archive entry while($zip_entry = zip_read($zip_handle)) { $resource = zip_entry_open($zip_handle, $zip_entry, "rb"); $file_name = zip_entry_name($zip_entry); if ($resource == true) { // Reading contents of a zip archive entry $file_content = zip_entry_read($zip_entry); echo("File: " . $file_name . " successfully opened. <br>"); echo("File content: " . $file_content); // Closing a zip archive entry zip_entry_close($zip_entry); } else echo("Failed to Open."); } // Closin zip file. zip_close($zip_handle); ?> |
Output:
File: articles/geeks successfully opened. File content: Welcome to GeeksforGeeks. It is a computer science portal where you can learn programming.
Program 2:
Suppose a zip file article.zip contains the following file:
geeks.txt
geeks1.txt
<?php // Opening a zip file $zip_handle = zip_open("C:/xampp/htdocs/articles.zip"); // Reading a zip archive entry while($zip_entry = zip_read($zip_handle)) { $resource = zip_entry_open($zip_handle, $zip_entry, "rb"); $file_name = zip_entry_name($zip_entry); if ($resource == true) { // Reading contents of a zip archive entry upto 150 bytes $file_content = zip_entry_read($zip_entry, 150); echo("File Name: " . $file_name . " is opened Successfully. <br>"); echo($file_content); echo("<br><br>"); // Closing a zip archive entry zip_entry_close($zip_entry); } else echo("Failed to Open."); } // Closing a zip archive zip_close($zip_handle); ?> |
Output:
File Name: articles/geeks is opened Successfully. Welcome to GeeksforGeeks. It is a computer science portal where you can learn programming. File Name: articles/geeks1 is opened Successfully. A Computer Science portal for geeks. It contains well written, well thought and well-explained computer science and programming articles, quizzes and many more.
Related Articles:
Reference: http://php.net/manual/en/function.zip-entry-read.php
Recommended Posts:
- How to call a function that return another function in JavaScript ?
- How to get the function name from within that function using JavaScript ?
- D3.js | d3.max() function
- p5.js | hex() function
- PHP | pi( ) Function
- p5.js | str() function
- PHP | Ds\Map map() Function
- p5.js | arc() Function
- PHP | each() Function
- p5.js | nf() Function
- p5.js | box() Function
- PHP | Ds\Map last() Function
- PHP | pow( ) Function
- PHP | Ds\Map first() Function
- PHP Ds\Map sum() Function
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.



