ZipArchive::setCompressionName

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

ZipArchive::setCompressionName β€” Π£ΡΡ‚Π°Π½ΠΎΠ²ΠΈΡ‚ΡŒ ΠΌΠ΅Ρ‚ΠΎΠ΄ сТатия записи, Π·Π°Π΄Π°Π½Π½ΠΎΠΉ ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ

ОписаниС

public function 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', 'НСкоторый тСкст');
$zip->addFromString('bar', 'НСкоторый Π΄Ρ€ΡƒΠ³ΠΎΠΉ тСкст');
$zip->setCompressionName('foo', ZipArchive::CM_STORE);
$zip->setCompressionName('bar', ZipArchive::CM_DEFLATE);
$zip->close();
echo
'Π³ΠΎΡ‚ΠΎΠ²ΠΎ';
} else {
echo
'ошибка';
}
?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #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
'Π³ΠΎΡ‚ΠΎΠ²ΠΎ';
} else {
echo
'ошибка';
}
?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 1 note

up
5
gajowy at agzeta dot pl ΒΆ
3 years 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.