zip_entry_compressedsize()
The zip_entry_compressedsize() function is a built-in function in PHP that is used to get the compressed size of a file in a zip archive. The compressed size is
The zip_entry_compressedsize() function is a legacy function from the PHP zip extension. It returns the compressed size of a single entry inside an open zip archive — that is, the number of bytes the file actually takes up on disk after compression, which is usually smaller than its original size.
This page explains what the function returned, how it was used, and — most importantly — what to use instead, because the function was deprecated in PHP 7.4 and removed in PHP 8.0. On any modern PHP installation (8.0 or later) calling it raises a fatal Error, so reach for the ZipArchive class shown below.
Why the compressed size matters
Every file stored in a zip archive has two sizes:
- Uncompressed size — the original byte length of the file. See
zip_entry_filesize(). - Compressed size — the byte length after the deflate algorithm shrinks it. That is what
zip_entry_compressedsize()returned.
Comparing the two tells you the compression ratio. Highly compressible text might shrink to 20% of its original size, while already-compressed data (JPEGs, MP3s) barely changes. Knowing the compressed size is useful for progress bars, storage planning, and deciding whether re-zipping is worthwhile.
Syntax
The syntax of the zip_entry_compressedsize() function is as follows:
syntax of the zip_entry_compressedsize() function in PHP
int zip_entry_compressedsize(resource $zip_entry)Where $zip_entry is the zip entry handle returned by zip_read() for the file in the zip archive. It returns the compressed size as an integer number of bytes. 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
$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. In a real script you would loop over zip_read() to walk every entry, and call zip_close() when finished.
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:
$zip = new ZipArchive;
if ($zip->open('example.zip') === true) {
$index = $zip->locateName('example.txt');
if ($index !== false) {
$stat = $zip->statIndex($index);
$compressedSize = $stat['comp_size']; // equivalent of zip_entry_compressedsize()
$uncompressedSize = $stat['size']; // original size
echo "Compressed: {$compressedSize} bytes\n";
echo "Uncompressed: {$uncompressedSize} bytes\n";
if ($uncompressedSize > 0) {
$ratio = round(100 * (1 - $compressedSize / $uncompressedSize), 1);
echo "Space saved: {$ratio}%\n";
}
}
$zip->close();
}The statIndex() method returns an associative array describing the entry; its comp_size key holds the compressed size that the legacy function used to return, and size holds the uncompressed size. Because both are available, you can compute the compression ratio directly, as shown above.
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, why the compressed size matters, provided a legacy example with basic error handling, and shown a modern alternative using the ZipArchive class with a compression-ratio calculation. For current PHP versions (8.0+), always prefer ZipArchive to avoid fatal errors and ensure compatibility.
See also
zip_entry_filesize()— get the uncompressed size of an entry.zip_entry_name()— get the name of an entry.zip_entry_compressionmethod()— see how the entry was compressed.zip_read()andzip_open()— open an archive and iterate its entries.- PHP Zip extension overview — the modern
ZipArchiveclass.