Home PHP Zip zip_entry_compressionmethod() PHP zip_entry_compressionmethod() Function 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.
// Deprecated (PHP 8.0+)
$method = zip_entry_compressionmethod($entry);
// Modern equivalent
$stat = $zip->statName('hello.txt');
$method = $stat['comp_method']; Call What 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 zip_entry_compressionmethod(resource $zip_entry): string|false Parameters Parameter Description zip_entry Required. 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 Value Description string The compression method name, for example "stored" or "deflated". false The method could not be read.
PHP Examples 1. Read comp_method From statName() 2. Turn a Method Number Into a Name 3. List Methods For Every Entry Example 1 — Read comp_method From statName() Store one entry without compression and read its method number.
<?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); hello.txt method: 0
Is CM_STORE: yes How It Works Create an archive with hello. txt and force the STORE method.Reopen it and read the stat array with statName(). Print the numeric comp_method value. 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
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;
} 0 => stored
8 => deflated
12 => bzip2
99 => other How It Works Define methodName() with an array of known method numbers. Return the name when the key exists. Use the null coalescing operator for an other fallback. Loop over sample numbers and print each mapping. Frequently Asked Questions Is zip_entry_compressionmethod() deprecated? 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().
What do the numbers 0 and 8 mean? 0 means stored (no compression), and 8 means deflated, the most common ZIP compression method.
Which constants should I compare against? Use ZipArchive::CM_STORE and ZipArchive::CM_DEFLATE rather than raw numbers, so your code is easier to read.
Can I change an entry's compression method? Yes, with ZipArchive::setCompressionName() or setCompressionIndex() before you close the archive.
Why might I see a method that is neither 0 nor 8? Other tools can create archives with methods such as bzip2. Always keep a fallback branch for unknown values.
About the author Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion