W3docs

zip_entry_compressionmethod()

The zip_entry_compressionmethod() function is a built-in function in PHP that is used to get the compression method of a file in a zip archive. The compression

Warning: zip_entry_compressionmethod() was deprecated in PHP 5.3.0 and removed in PHP 8.0.0 (the old procedural zip_* reading API was dropped). It is no longer available in modern PHP. This page is kept for historical reference. On any supported PHP version, use the ZipArchive class instead, as shown below.

The zip_entry_compressionmethod() function was a built-in PHP function used to read the compression method of a single entry (file) inside a zip archive. The compression method is the algorithm that was used to store that entry — most commonly deflate (the default) or store (no compression).

This page explains what the legacy function did and, more importantly, how to get the same information in modern PHP with ZipArchive.

Why the compression method matters

Every file in a zip archive records how it was compressed. Knowing the method lets you:

  • Audit an archive — confirm whether files are actually compressed or just stored.
  • Estimate workstore entries decompress instantly; deflate entries cost CPU.
  • Debug size issues — already-compressed data (JPEG, PNG, MP4) is often stored rather than re-compressed, because re-deflating it saves nothing.

The two methods you will see in practice are:

ConstantCodeMeaning
ZipArchive::CM_STORE0The entry is stored uncompressed.
ZipArchive::CM_DEFLATE8The entry is compressed with the deflate algorithm (the default).

Legacy syntax

The original function took a zip entry handle returned by zip_read():

int zip_entry_compressionmethod(resource $zip_entry)

Where $zip_entry is the zip entry resource for a file in the archive. This required the legacy procedural zip API and no longer exists, so do not use it in new code.

Modern approach with ZipArchive

The portable, version-safe way to read the compression method is ZipArchive::statIndex(). It returns an array whose comp_method key holds the numeric method code (0 for store, 8 for deflate). The following example is fully self-contained — it builds a small archive, then reports the method of every entry:

<?php
$file = sys_get_temp_dir() . '/example.zip';

// 1. Build a zip with one compressed and one stored entry.
$zip = new ZipArchive();
if ($zip->open($file, ZipArchive::CREATE | ZipArchive::OVERWRITE) === true) {
    $zip->addFromString('report.txt', str_repeat('compress me ', 100));
    $zip->addFromString('raw.txt', 'left as-is');
    $zip->setCompressionName('raw.txt', ZipArchive::CM_STORE); // force no compression
    $zip->close();
}

// 2. Reopen and read each entry's compression method.
$labels = [
    ZipArchive::CM_STORE   => 'stored (no compression)',
    ZipArchive::CM_DEFLATE => 'deflate',
];

$zip = new ZipArchive();
if ($zip->open($file) === true) {
    for ($i = 0; $i < $zip->numFiles; $i++) {
        $stat   = $zip->statIndex($i);
        $code   = $stat['comp_method'];
        $method = $labels[$code] ?? "unknown ($code)";
        echo "{$stat['name']} => {$method}\n";
    }
    $zip->close();
}

Output:

report.txt => deflate
raw.txt => stored (no compression)

statIndex() reads from the central directory, so it is cheap — it never decompresses the file.

Shorter alternative: getCompressionName()

If your PHP is built against libzip 1.0 or newer, ZipArchive::getCompressionName() returns the method name as a string directly:

$zip = new ZipArchive();
if ($zip->open('example.zip') === true) {
    // Name of the compression method used for the first entry.
    echo $zip->getCompressionName(0); // e.g. "deflate"
    $zip->close();
}

This is more readable, but getCompressionName() is not present in every build. Prefer the statIndex() approach above when you need portability across servers.

Conclusion

zip_entry_compressionmethod() belonged to PHP's removed procedural zip API and should never be used in new code. To read the compression method of a zip entry in modern PHP, use ZipArchive::statIndex() and inspect its comp_method value (0 = store, 8 = deflate), or getCompressionName() where it is available.

To keep working with zip archives, see Working with the Zip extension, zip_entry_name() for an entry's name, and zip_entry_compressedsize() for its compressed size.

Practice

Practice
What does the zip_entry_compressionmethod() function in PHP do?
What does the zip_entry_compressionmethod() function in PHP do?
Was this page helpful?