Skip to content

zip_entry_compressedsize()

The zip_entry_compressedsize() function is a legacy function from the PHP zip extension. It was deprecated in PHP 7.4 and removed in PHP 8.0. The compressed size is the size of the file after it has been compressed using the zip compression algorithm.

Syntax

The syntax of the zip_entry_compressedsize() function is as follows:

syntax of the zip_entry_compressedsize() function in PHP

php
int zip_entry_compressedsize(resource $zip_entry)

Where $zip_entry is the zip entry handle for the file in the zip archive. Note that this function belongs to the legacy zip extension and is no longer available in PHP 8.0+.

Usage Examples

Let's take a look at a practical example of using zip_entry_compressedsize() in PHP.

Example: Getting the Compressed Size of a File in a Zip Archive

Suppose you have opened a zip archive using the legacy PHP zip functions and want to get the compressed size of a file in the archive. You can use the zip_entry_compressedsize() function to do this, like this:

Getting the Compressed Size of a File in a Zip Archive in PHP

php
$zip = zip_open("example.zip");
if ($zip === false) {
    die("Failed to open zip archive.");
}

$zip_entry = zip_read($zip);
if ($zip_entry === false) {
    die("Failed to read zip entry.");
}

// get the compressed size of the file
$compressed_size = zip_entry_compressedsize($zip_entry);

echo "The compressed size of the file is: " . $compressed_size . " bytes.";

This code opens a zip archive file example.zip using zip_open(). We then read a file in the archive using zip_read() and get its compressed size using zip_entry_compressedsize(). Finally, the compressed size is outputted to the user.

Modern Alternative: Using ZipArchive

Since the legacy zip_* functions were removed in PHP 8.0, it is recommended to use the built-in ZipArchive class instead. Here is how you can get the compressed size of a file using ZipArchive:

php
$zip = new ZipArchive;
if ($zip->open('example.zip') === true) {
    $index = $zip->locateName('example.txt');
    if ($index !== false) {
        $stat = $zip->statIndex($index);
        $compressed_size = $stat['comp_size'];
        echo "The compressed size of the file is: " . $compressed_size . " bytes.";
    }
    $zip->close();
}

Conclusion

In this article, we've discussed the legacy zip_entry_compressedsize() function and how it was used to get the compressed size of a file in a zip archive. We've explained its syntax, provided a legacy example with basic error handling, and shown a modern alternative using the ZipArchive class. For current PHP versions (8.0+), always prefer ZipArchive to avoid fatal errors and ensure compatibility.

Practice

What is the functionality of the zip_entry_compressedsize() function in PHP?

Dual-run preview — compare with live Symfony routes.