PHP | zip_entry_close() Function

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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



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.