Tuesday, November 19, 2024
Google search engine
HomeLanguagesPHP | zip_entry_open() Function

PHP | zip_entry_open() Function

The zip_entry_open() function is an inbuilt function in PHP which is used to open a zip entry archive for reading. Opening a file or a directory in a zip archive with the zip_entry_open function creates a new stream and establishes a connection between the stream and a file or a directory in a Zip Archive. The zip resource and the zip entry resource to be opened and sent as parameters to the zip_entry_open() function and it returns True on success and False on failure. 

Syntax:

bool zip_entry_open( $zip, $zip_entry, $mode )

Parameters: This function accepts three parameters as mentioned above and described below:

  • $zip: It is a mandatory parameter which specifies the zip resource to be read.
  • $zip_entry: It is a mandatory parameter which specifies the zip entry resource.
  • $mode: It is an optional parameter which the access type of the required for the zip archive.

Return Value: It returns True on success or False on failure. 

Errors And Exceptions:

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

Below programs illustrate the zip_entry_open() function in PHP: 

Program 1:

Suppose a zip file articles.zip contains the following file: neveropen.txt

php




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


Output:

Zip file: articles/neveropen open successfully 
Zip file: articles/neveropen closed successfully

Program 2:

Suppose a zip file articles.zip contains the following files: neveropen.txt neveropen1.txt

php




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


Output:

Zip file: articles/neveropen open successfully
Zip file: articles/neveropen closed successfully 

Zip file: articles/neveropen1 open successfully
Zip file: articles/neveropen1 closed successfully

Related Articles:

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments