Skip to content
Related Articles

Related Articles

PHP | zip_entry_read() Function
  • Last Updated : 26 Jul, 2018

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :