Open In App

PHP | zip_entry_close() Function

Last Updated : 18 Jul, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

The zip_entry_close() function is an inbuilt function in PHP which is used to close a zip archive opened by the zip_entry_open() function. The zip_entry_close() causes the stream to be closed and the connection to the corresponding Zip Archive Entry which may be a file or a directory within the Zip Archive to be broken. The zip entry resource which has to be closed is sent as a parameter to the zip_entry_close() function.

Syntax:

 bool zip_entry_close ( $zip_entry )

Parameters: The zip_entry_close() function accepts single parameter $zip_entry. It is a mandatory parameter which specifies the zip entry resource.

Return Value: It returns true on success or False on Failure.

Errors And Exceptions:

  • The zip entry archive to be closed must be opened first by using the PHP zip_entry_open() function otherwise the PHP zip_entry_close() function produces a PHP warning.
  • The zip_entry_close() function returns an ER_OPEN error if the zip archive is invalid.
  • The zip_entry_close() function returns an ER_NOZIP error if the zip archive is empty.

Suppose a zip file article.zip contains the following file:
content.xlsx

Below programs illustrate the zip_entry_close() function in PHP:

Program 1:




<?php
  
// Opening a zip archive
$zip_handle = zip_open("C:/xampp/htdocs/article.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);
   
// Closing a zip entry archive 
$flag = zip_entry_close($zip_entry);
  
if ($flag == true) 
    echo("Zip Entry Archive: " . $file . " has been closed successfully. ");
else
    echo("Zip Entry Archive: " . $file . " cannot be closed.");
   
zip_close($zip_handle);
?>


Output:

Zip Entry Archive: article/content.xlsx has been closed successfully.

Suppose a zip file article.zip contains the following files:
content.xlsx
gfg.pdf
image.jpeg

Program 2:




<?php
  
// Opening a zip archive
$zip_handle = zip_open("C:/xampp/htdocs/article.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 Entry Archive: " . $file_name
                  " has been opened successfully." . "<br>");
  
            // Closing a zip archive entry
            $flag = zip_entry_close($zip_entry);
              
            if ($flag == true) 
                echo("Zip Entry Archive: " . $file_name .
                  " has been closed successfully." . "<br>");
            else
                echo("Zip Entry Archive: " . $file_name .
                              " cannot be closed." . "<br>");
        
        else
            echo("Zip Entry Cannot be opened."); 
    
  
    // Closing a zip archive
    zip_close($zip_handle);
}
else
    echo("Failed to Open" . $zip_handle );
?>


Output:

Zip Entry Archive: article/content.xlsx has been opened successfully.
Zip Entry Archive: article/content.xlsx has been closed successfully.
Zip Entry Archive: article/gfg.pdf has been opened successfully.
Zip Entry Archive: article/gfg.pdf has been closed successfully.
Zip Entry Archive: article/image.jpeg has been opened successfully.
Zip Entry Archive: article/image.jpeg has been closed successfully.

Reference: http://php.net/manual/en/function.zip-entry-close.php



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads