PHP 8.4.2 Released!

ZipArchive::setCompressionName

(PHP 7, PHP 8, PECL zip >= 1.13.0)

ZipArchive::setCompressionName名前で定義されたエントリの圧縮メソッドを設定する

説明

public ZipArchive::setCompressionName(string $name, int $method, int $compflags = 0): bool

名前で定義されたエントリの圧縮メソッドを設定します。

パラメータ

name

エントリの名前

method

圧縮メソッド。 ZipArchive::CM_* のいずれかです。

compflags

圧縮レベル

戻り値

成功した場合に true を、失敗した場合に false を返します。

例1 アーカイブに異なる圧縮メソッドでファイルを追加する

<?php
$zip
= new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if (
$res === TRUE) {
$zip->addFromString('foo', 'Some text');
$zip->addFromString('bar', 'Some other text');
$zip->setCompressionName('foo', ZipArchive::CM_STORE);
$zip->setCompressionName('bar', ZipArchive::CM_DEFLATE);
$zip->close();
echo
'ok';
} else {
echo
'failed';
}
?>

例2 ファイルを追加し、圧縮メソッドを設定する

<?php
$zip
= new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if (
$res === TRUE) {
$zip->addFile('foo.jpg', 'bar.jpg');
$zip->setCompressionName('bar.jpg', ZipArchive::CM_XZ);
$zip->close();
echo
'ok';
} else {
echo
'failed';
}
?>
add a note

User Contributed Notes 1 note

up
1
gajowy at agzeta dot pl
1 year ago
The compression level (compflags) option is, generally speaking, an integer value between 0 and 9. The behavior of the system depends on the selected method and the value of compflags and can sometimes be not you expected.

The result below is from PHP 8.1 on Windows platform and may possibly be different on other systems or versions:

- for the CM_DEFAULT method, always CM_DEFLATE is used with level 9, regardless of what you put as compflags,

- for the CM_STORE method, for compflags 0-9 you get the same result, which is obvious because the method itself means "no compression at all". However, for compflags>9, surprisingly the CM_DEFLATE method is used instead with compression level 9.

- for CM_DEFLATE method, 1 means the fastest and weakest compression, while 9 - the slowest and strongest one. compflags=0 and compflags>9 works as it if were with compflags=9,

- for CM_BZIP2 method, 1 means the fastest and weakest compression, while 9 - the slowest and strongest one. compflags=0 works like compflag=9, and if you use compflags>9, the method will surprisingly switch to CM_DEFLATE level 9,

- for CM_XZ method, 0 means the fastest and weakest compression, while 9 - the slowest and strongest one. For compflags>9 the method surprisingly switch to CM_DEFLATE level 9.
To Top