ZipArchive::setArchiveComment

(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL zip >= 1.4.0)

ZipArchive::setArchiveComment β€” УстанавливаСт ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ ΠΊ ZIP-Π°Ρ€Ρ…ΠΈΠ²Ρƒ

ОписаниС

public function ZipArchive::setArchiveComment(string $comment): bool

УстанавливаСт ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ ΠΊ ZIP-Π°Ρ€Ρ…ΠΈΠ²Ρƒ.

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

comment
Π‘ΠΎΠ΄Π΅Ρ€ΠΆΠ°Π½ΠΈΠ΅ коммСнтария.

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 Π‘ΠΎΠ·Π΄Π°Π½ΠΈΠ΅ Π°Ρ€Ρ…ΠΈΠ²Π° ΠΈ установка коммСнтария

<?php
$zip
= new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if (
$res === TRUE) {
$zip->addFromString('test.txt', 'содСрТимоС Ρ„Π°ΠΉΠ»Π°');
$zip->setArchiveComment('Π½ΠΎΠ²Ρ‹ΠΉ ΠΊΠΎΠΌΠΌΠ΅Π½Ρ‚Π°Ρ€ΠΈΠΉ Π°Ρ€Ρ…ΠΈΠ²Π°');
$zip->close();
echo
'Π³ΠΎΡ‚ΠΎΠ²ΠΎ';
} else {
echo
'ошибка';
}
?>
οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

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

up
6
stanislav dot eckert at vizson dot de ΒΆ
11 years ago
Please note that ZIP archives do not support unicode encodings like UTF-8, so multi-byte characters cannot be shown in standard ZIP viewers like WinRAR or 7-zip. However, the text will stored as-is, so it is at least possible to display UTF-8 comments in your own desktop or web applications. If you want to test with PHP and output in a browser, don't forget to set page charset to UTF-8 too:

header("Content-Type: text/plain; charset=utf-8");
up
1
solrac at ragnarockradio dot com ΒΆ
9 years ago
Zip archives are encoded in ISO-8859-1 when stored but comments seem to be added in UTF-8 everytime. So...

<?php
   $zip->setArchiveComment("PeΓ±a");    //outputs "Peña" as the comment.

   $zip->setArchiveComment("PeΓ±a");    //outputs "NULL" as the comment / no comment is displayed.
?>

Using mb_internal_encoding() or mb_http_output() wont change this behavior.
At the end you can fix your corrupted comment using something like str_replace();

Consider this:

<?php
$zip = new ZipArchive;
$res = $zip->open('test.zip', ZipArchive::CREATE);
if ($res === TRUE) {
    $zip->addFromString('test.txt', 'file content goes here');
    $zip->setArchiveComment('PeΓ±a'); //outputs "Peña" as the comment.
    $zip->close();
    $file = file_get_contents('test.zip');
    file_put_contents('test.zip', str_replace("PeΓ±a", utf8_decode("PeΓ±a"), $file)); //outputs "PeΓ±a" as the comment. FIXED!

    echo 'ok';
} else {
    echo 'failed';
}
?>
up
1
poetbi at boasoft dot cn ΒΆ
2 years ago
ZipArchive (using libzip) encodes comments in UTF-8/ASCII, but some softwares on Windows show comments in ANSI (such as GBK...), so we should :

<?php
    $_charset = 'GBK';
    $file = 'D:/boaphp.zip';
    $comment = 'δΈ­ζ–‡ABC123';
    
    $zip = new ZipArchive;
    $res = $zip->open($file, ZipArchive::CREATE);
    if ($res) {
        //add files here

        if($_charset){ //for Winrar, 7z...
            $zip->close();
            
            $str = mb_convert_encoding($comment, $_charset, 'UTF-8');
            $fh = fopen($file, 'r+b');
            fseek($fh, -2, SEEK_END);
            $str = pack('v', strlen($str)) . $str;
            fwrite($fh, $str);
            fclose($fh);
        }else{ //for PHP: $zip->getArchiveComment()
            $zip->setArchiveComment($comment);
            $zip->close();
        }
    }
?>