PHP | zip_entry_filesize() Function

The zip_entry_filesize() function is an inbuilt function in PHP which is used to return the original file size of a zip archive entry before compression. The zip entry resource is to be read and sent as a parameter to the zip_entry_filesize() function and it returns the value in bytes on Success.

Syntax:

int zip_entry_filesize( $zip_entry )

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

Return Value: It returns the value in bytes on Success.

Errors And Exceptions:

  • The zip_entry_filesize() returns the size in bytes of a file before compression only on Success otherwise it returns a PHP warning.
  • The zip_entry_filesize() function returns an ER_OPEN error if the zip archive is invalid.
  • The zip_entry_filesize() function returns an ER_NOZIP error if the zip archive is empty.

Below programs illustrate the zip_entry_filesize() 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 file
$zip_handle = zip_open("C:/xampp/htdocs/article.zip");
  
// Reading a zip entry archive 
$zip_entry = zip_read($zip_handle); 
$file = zip_entry_name($zip_entry);
  
// Reading file size before compression
$size = zip_entry_filesize($zip_entry);
  
// Displaying the file ans its size
echo("File Name: " . $file . "<br>Size:" . $size . " Bytes");
zip_close($zip_handle);
?>

chevron_right


Output:

File Name: article/content.xlsx
Size: 9420 Bytes

Program 2:

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

Directory: img

  • geeksforgeeks.png
  • geeksforgeeks1.png

content.xlsx
gfg.pdf
image.jpeg

filter_none

edit
close

play_arrow

link
brightness_4
code

<?php
  
// Opening a zip file
$zip_handle = zip_open("C:/xampp/htdocs/article.zip");
  
if(is_resource($zip_handle)) 
    while($zip_entry = zip_read($zip_handle)) 
    
        $file = zip_entry_name($zip_entry);
         
        // Checking the file size of a zip 
        // archive entry before compression  
        $size = zip_entry_filesize($zip_entry);
        echo("File Name: " . $file . "<br>Size: " . $size . " Bytes<br>");
    
  
    // closing the zip archive
    zip_close($zip_handle);
else
   echo("Zip archive cannot be read.");
?>

chevron_right


Output:

File Name: article/content.xlsx
Size: 9420 Bytes
File Name: article/gfg.pdf
Size: 621936 Bytes
File Name: article/image.jpeg
Size: 159263 Bytes
File Name: article/img/
Size: 0 Bytes
File Name: article/img/geeksforgeeks.png
Size: 751 Bytes
File Name: article/img/geeksforgeeks1.png
Size: 337 Bytes

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