Open In App

PHP | zip_entry_compressionmethod() Function

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

The zip_entry_compressionmethod() function is an inbuilt function in PHP which is used to return the compression method of a file or a directory from a zip archive entry. The zip entry resource which has to be read is sent as a parameter to the zip_entry_compressionmethod() function and it returns the compression method on Success.

Compression methods are of seven types which are as follows :

  • uncompressed
  • Shrunk
  • Deflate
  • Reduced (1 to 4)
  • Tokenizing
  • Imploded
  • BZIP2

The default compression method of the zip archive file is deflated.

Syntax:

string zip_entry_compressionmethod( $zip_entry )

Parameters: This function accepts single parameter $zip_entry. It is a mandatory parameter which specifies the zip entry resource.

Return Value: It returns the compression method of a file or directory of the specified zip archive entry on success otherwise a PHP Warning.

Errors And Exceptions

  • The zip_entry_compressionmethod() returns the compressed method of a file or a directory only on Success otherwise it returns a PHP warning.
  • The zip_entry_compressionmethod() function returns an ER_OPEN error if the zip archive is invalid.
  • The zip_entry_compressionmethod() function returns an ER_NOZIP error if the zip archive is empty.

Below programs illustrate the zip_entry_compressionmethod() function in PHP:

Program 1:

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




<?php
  
// Opening a zip archive
$zip_handle = zip_open("C:/xampp/htdocs/article.zip");
   
// Reading a zip archive
$zip_entry = zip_read($zip_handle); 
$file = zip_entry_name($zip_entry);
   
// Checking the  compression method
$comp_type = zip_entry_compressionmethod($zip_entry);
echo("File Name: " . $file . "=>" . $comp_type);
   
// Closing the zip archive
zip_close($zip_handle);
?>


Output:

File Name: article/content.xlsx => deflated

Program 2:

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




<?php
  
// Opening a zip archive
$zip_handle = zip_open("C:/xampp/htdocs/article.zip");
  
if(is_resource($zip_handle))
    // Reading a zip archive
    while($zip_entry = zip_read($zip_handle)) 
    
        $file = zip_entry_name($zip_entry);
          
        // Checking the compression method
        $comp_type = zip_entry_compressionmethod($zip_entry);
          
        echo("File Name: " . $file . "  =>  " . $comp_type . "<br>");
   
     
    // Closing the zip archive
    zip_close($zip_handle);
else
    echo("Zip archive cannot be opened.");
   
?>


Output:

File Name: article/art.zip => stored
File Name: article/content.xlsx => deflated
File Name: article/gfg.pdf => deflated
File Name: article/image.jpeg => deflated

Related Articles:

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads