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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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



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.