PHP zip_entry_compressedsize() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

Definition

In PHP, zip_entry_compressedsize() returns how many bytes a ZIP entry takes up inside the archive after compression.

It is deprecated as of PHP 8.0. With ZipArchive, call statName() or statIndex() and read the comp_size key.

Syntax

Call zip_entry_compressedsize() with the values you need. Start with this line.

PHP
// Deprecated (PHP 8.0+)
$bytes = zip_entry_compressedsize($entry);

// Modern equivalent
$stat = $zip->statName('hello.txt');
$bytes = $stat['comp_size'];
CallWhat it does
zip_entry_compressedsize($entry)Deprecated: returns the compressed size of the current entry in bytes.
@zip_entry_compressedsize($entry)Deprecated: the @ operator hides the E_DEPRECATED notice only.
$zip->statName('hello.txt')['comp_size']Modern ZipArchive: reads the compressed size from the entry's stat array.

zip_entry_compressedsize() is deprecated in PHP 8.0. Compare comp_size with size from ZipArchive::statName() to see how much space compression saved.

Library declarations
PHP
zip_entry_compressedsize(resource $zip_entry): int|false

Parameters

ParameterDescription
zip_entryRequired. A directory entry resource returned by zip_read(). In ZipArchive you use the entry name or index instead.

Return Value

Return ValueDescription
intThe compressed size of the entry in bytes.
falseThe size could not be read.

PHP Examples

Example 1 — Read comp_size From statName()

Store one entry without compression so the numbers are always the same, then read size and comp_size.

PHP
<?php
// Legacy (deprecated since PHP 8.0):
// $bytes = zip_entry_compressedsize($entry);

$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('hello.txt', "Hello zip\n");
$zip->setCompressionName('hello.txt', ZipArchive::CM_STORE);
$zip->close();

$zip = new ZipArchive();
$zip->open($path);
$stat = $zip->statName('hello.txt');
echo 'size: ', $stat['size'], PHP_EOL;
echo 'comp_size: ', $stat['comp_size'], PHP_EOL;
$zip->close();

unlink($path);

How It Works

  1. Create an archive and add hello. txt.
  2. Force the STORE method so no compression is applied.
  3. Reopen the archive and call statName() on hello. txt.
  4. Print size and comp_size, then delete the temp file.

Example 2 — Repeated Text Compresses Well

Highly repetitive text shrinks a lot with DEFLATE. The script prints a yes/no check instead of an exact byte count.

PHP
<?php
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('big.txt', str_repeat('A', 1000));
$zip->setCompressionName('big.txt', ZipArchive::CM_DEFLATE);
$zip->close();

$zip = new ZipArchive();
$zip->open($path);
$stat = $zip->statName('big.txt');
echo 'size: ', $stat['size'], PHP_EOL;
echo 'Compressed smaller: ', $stat['comp_size'] < $stat['size'] ? 'yes' : 'no', PHP_EOL;
$zip->close();

unlink($path);

How It Works

  1. Add an entry made of one thousand repeated letters.
  2. Ask ZipArchive to compress it with DEFLATE.
  3. Reopen the archive and read the stat array.
  4. Print the original size and whether comp_size is smaller.

Example 3 — Stored vs Deflated Entry

Add the same text twice, once stored and once deflated, and compare their compressed sizes.

PHP
<?php
$path = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'codetofun_' . uniqid() . '.zip';
$text = str_repeat('hello ', 100);

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('stored.txt', $text);
$zip->addFromString('deflated.txt', $text);
$zip->setCompressionName('stored.txt', ZipArchive::CM_STORE);
$zip->setCompressionName('deflated.txt', ZipArchive::CM_DEFLATE);
$zip->close();

$zip = new ZipArchive();
$zip->open($path);
for ($i = 0; $i < $zip->numFiles; $i++) {
    $stat = $zip->statIndex($i);
    $same = $stat['comp_size'] === $stat['size'];
    echo $stat['name'], ': comp_size equals size? ', $same ? 'yes' : 'no', PHP_EOL;
}
$zip->close();

unlink($path);

How It Works

  1. Build a 600-byte string by repeating the word hello.
  2. Add it as stored. txt and deflated.txt, choosing a method for each.
  3. Reopen the archive and loop with statIndex().
  4. Print whether comp_size equals size for each entry.

Frequently Asked Questions

Yes. It belongs to the procedural zip_* API deprecated as of PHP 8.0. Use ZipArchive::statName() or statIndex().
The comp_size key. The size key holds the original uncompressed size.
For very small or already compressed data it can be about the same or slightly larger, depending on the method and tool that built the archive.
Subtract comp_size from size, or divide comp_size by size to get a ratio. Check that size is not zero before dividing.
A stored entry is not compressed, so comp_size equals size.

Next: zip_entry_compressionmethod()

zip_entry_compressionmethod() tells you which compression method a ZIP entry uses, such as.

zip_entry_compressionmethod() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful