PHP | zip_open() Function

The zip_open() function is an inbuilt function in PHP which is used to open a zip archive for reading. The zip_open() function creates a new stream and establishes a connection between the stream and a Zip Archive. The filename is sent as a parameter to the zip_open() function and it returns a valid resource handler if the zip archive file is opened successfully otherwise it returns an Error.

Syntax:

zip_open( $filename )

Parameters: This function accepts single parameter $filename which is mandatory. It is used to specify the zip resource to be opened.



Return Value: It returns a valid resource handler if the zip archive file is opened successfully otherwise it returns an Error.

Errors And Exceptions:

  • The zip_open() function returns an ER_OPEN error if the zip archive is invalid.
  • The zip_open() function returns an ER_NOZIP error if the zip archive is empty.

Below programs illustrate the zip_open() function in PHP:

Suppose a zip file article.zip contains the following files:

article.zip
content.xlsx
gfg.pdf
image.jpeg

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Opening zip file
$my_zip = zip_open("article.zip");
  
if(is_resource($my_zip))
    echo("Zip file opened successfully.");
      
    // Closing zip file
    zip_close($my_zip);
else
    echo($my_zip . "file can not be opened");
?>

chevron_right


Output:

Zip file opened successfully.

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Opening zip file
$my_zip= zip_open("article.zip");
  
if(is_resource($my_zip)) 
    while($zipfiles = zip_read($my_zip)) 
    
        $file_name = zip_entry_name($zipfiles);
        echo("File Name: " . $file_name . "<br>");
    
      
    // Closing zip file
    zip_close($my_zip);
else
    echo($my_zip . "file Can not be opened");
?>

chevron_right


Output:

File Name: article/article.zip
File Name: article/content.xlsx
File Name: article/gfg.pdf
File Name: article/image.jpeg

Related Articles:

Reference: http://php.net/manual/en/function.zip-open.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.