PHP | zip_entry_compressionmethod() Function

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

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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

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))
    // 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.");
   
?>

chevron_right


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



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.