PHP zip_entry_compressionmethod() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Zip

Definition

In PHP, zip_entry_compressionmethod() tells you which compression method a ZIP entry uses, such as stored or deflated.

It is deprecated as of PHP 8.0. With ZipArchive, read the comp_method key from statName() or statIndex() and compare it with ZipArchive::CM_STORE or ZipArchive::CM_DEFLATE.

Syntax

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

PHP
// Deprecated (PHP 8.0+)
$method = zip_entry_compressionmethod($entry);

// Modern equivalent
$stat = $zip->statName('hello.txt');
$method = $stat['comp_method'];
CallWhat it does
zip_entry_compressionmethod($entry)Deprecated: returns the compression method name for the current entry.
@zip_entry_compressionmethod($entry)Deprecated: the @ operator hides the E_DEPRECATED notice only.
$zip->statName('hello.txt')['comp_method']Modern ZipArchive: returns the method as an integer such as 0 (stored) or 8 (deflated).

zip_entry_compressionmethod() is deprecated in PHP 8.0. ZipArchive gives you a number instead of a word: 0 is ZipArchive::CM_STORE and 8 is ZipArchive::CM_DEFLATE.

Library declarations
PHP
zip_entry_compressionmethod(resource $zip_entry): string|false

Parameters

ParameterDescription
zip_entryRequired. A directory entry resource returned by zip_read(). In ZipArchive you pass the entry name or index to a stat method instead.

Return Value

Return ValueDescription
stringThe compression method name, for example "stored" or "deflated".
falseThe method could not be read.

PHP Examples

Example 1 — Read comp_method From statName()

Store one entry without compression and read its method number.

PHP
<?php
// Legacy (deprecated since PHP 8.0):
// echo zip_entry_compressionmethod($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 'hello.txt method: ', $stat['comp_method'], PHP_EOL;
echo 'Is CM_STORE: ', $stat['comp_method'] === ZipArchive::CM_STORE ? 'yes' : 'no', PHP_EOL;
$zip->close();

unlink($path);

How It Works

  1. Create an archive with hello. txt and force the STORE method.
  2. Reopen it and read the stat array with statName().
  3. Print the numeric comp_method value.
  4. Compare it with ZipArchive::CM_STORE and delete the temp file.

Example 2 — Turn a Method Number Into a Name

No archive needed: map common method numbers to readable names, with a fallback for unknown values.

PHP
<?php
function methodName(int $method): string
{
    $names = [
        0 => 'stored',
        8 => 'deflated',
        12 => 'bzip2',
    ];
    return $names[$method] ?? 'other';
}

foreach ([0, 8, 12, 99] as $m) {
    echo $m, ' => ', methodName($m), PHP_EOL;
}

How It Works

  1. Define methodName() with an array of known method numbers.
  2. Return the name when the key exists.
  3. Use the null coalescing operator for an other fallback.
  4. Loop over sample numbers and print each mapping.

Example 3 — List Methods For Every Entry

Add one stored and one deflated entry, then print the method name for each.

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

$zip = new ZipArchive();
$zip->open($path, ZipArchive::CREATE | ZipArchive::OVERWRITE);
$zip->addFromString('a.txt', str_repeat('A', 500));
$zip->addFromString('b.txt', str_repeat('B', 500));
$zip->setCompressionName('a.txt', ZipArchive::CM_STORE);
$zip->setCompressionName('b.txt', ZipArchive::CM_DEFLATE);
$zip->close();

$zip = new ZipArchive();
$zip->open($path);
for ($i = 0; $i < $zip->numFiles; $i++) {
    $stat = $zip->statIndex($i);
    $label = $stat['comp_method'] === ZipArchive::CM_STORE ? 'stored' : 'deflated';
    echo $stat['name'], ' => ', $label, PHP_EOL;
}
$zip->close();

unlink($path);

How It Works

  1. Add a. txt and b.txt with 500 repeated letters each.
  2. Set a. txt to CM_STORE and b.txt to CM_DEFLATE.
  3. Reopen the archive and loop with statIndex().
  4. Print each entry name with a stored or deflated label.

Frequently Asked Questions

Yes. It is part of the procedural zip_* API deprecated as of PHP 8.0. Use the comp_method value from ZipArchive::statName() or statIndex().
0 means stored (no compression), and 8 means deflated, the most common ZIP compression method.
Use ZipArchive::CM_STORE and ZipArchive::CM_DEFLATE rather than raw numbers, so your code is easier to read.
Yes, with ZipArchive::setCompressionName() or setCompressionIndex() before you close the archive.
Other tools can create archives with methods such as bzip2. Always keep a fallback branch for unknown values.

Next: zip_entry_filesize()

zip_entry_filesize() returns the original, uncompressed size of a ZIP entry in bytes.

zip_entry_filesize() 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