Current Location: Home> Latest Articles> How to Use zip_entry_compressedsize()? Practical Tips for Reading Compressed Files with zip_read Function

How to Use zip_entry_compressedsize()? Practical Tips for Reading Compressed Files with zip_read Function

M66 2025-06-26

When working with ZIP compressed files, PHP offers a convenient set of function interfaces. The zip_entry_compressedsize() function is one of them, which helps developers read the "compressed size" of data within a ZIP archive. This article demonstrates how to effectively read the contents of compressed files and related compression details by combining it with the zip_read() function.

1. What is zip_entry_compressedsize()

zip_entry_compressedsize() is a function used to retrieve the compressed size of a single entry inside a ZIP file. The function takes a resource handle returned by zip_read() as its parameter and returns the compressed size of the corresponding entry in bytes.

The function prototype is as follows:

int zip_entry_compressedsize(resource $zip_entry)

This function is typically used when iterating through ZIP archive entries to obtain each file's compressed byte size, which can then be used to analyze compression rates, evaluate performance, or preallocate buffers.

2. Complete Example Using zip_read()

The following code demonstrates how to open a ZIP file, read entries one by one, and retrieve the compressed size of each file.

$zipPath = '/path/to/example.zip';
<p>$zip = zip_open($zipPath);<br>
if (is_resource($zip)) {<br>
while ($zip_entry = zip_read($zip)) {<br>
echo "Filename: " . zip_entry_name($zip_entry) . "\n";<br>
echo "Compressed size: " . zip_entry_compressedsize($zip_entry) . " bytes\n";<br>
echo "Original size: " . zip_entry_filesize($zip_entry) . " bytes\n";</p>
        $contents = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
        // You can process the contents here, such as saving or analyzing
        zip_entry_close($zip_entry);
    }
    
    echo "-----------------------------\n";
}
zip_close($zip);

} else {
echo "Failed to open ZIP file.\n";
}

Explanation:

3. Use Case Examples

1. Display Compression Ratio

You can calculate the compression ratio using the sizes before and after compression, for example:

$compressed = zip_entry_compressedsize($zip_entry);
$original = zip_entry_filesize($zip_entry);
$rate = $original > 0 ? round(($compressed / $original) * 100, 2) : 0;
echo "Compression ratio: $rate%\n";

2. File Filtering and Classification

You can quickly filter large files or files with high compression ratios based on their compressed sizes:

if (zip_entry_compressedsize($zip_entry) > 1024 * 100) {
    echo "This is a large file, compressed size greater than 100KB.\n";
}

4. Real Case: Reading Contents from a Remote ZIP File

If the ZIP file comes from a remote location, for example https://m66.net/files/archive.zip, you can first download it to a local temporary directory using file_get_contents() before processing it:

$tempFile = tempnam(sys_get_temp_dir(), 'zip');
file_put_contents($tempFile, file_get_contents('https://m66.net/files/archive.zip'));
<p>$zip = zip_open($tempFile);<br>
if (is_resource($zip)) {<br>
// Follow the same logic as above<br>
zip_close($zip);<br>
unlink($tempFile);<br>
}<br>

5. Notes

  • This function has compatibility issues in some older PHP versions. It is recommended to use the ZipArchive class instead of the zip_* functions in PHP 7 and above. However, in some cases, zip_entry_compressedsize() can be more straightforward.

  • When handling ZIP archives with many entries, memory management and error handling are advised to prevent resource leaks or server crashes.

6. Conclusion

By using zip_entry_compressedsize() in combination with zip_read(), developers can easily access detailed information within ZIP archives. This approach is useful for log analysis, remote package content extraction, online decompression services, and more. Mastering these basic tools will make PHP file processing more efficient and effective.