Search code examples
phpzipphp-7php-ziparchivewinrar

Multi line comment in ZipArchive


I am trying to set a multi-line comment with ZipArchive.Below is a simplified demo.

<?php
$comment = "File \t Stats\r\n";
$comment .= "Second line ...some another text \r\n";
$zip->setArchiveComment($comment);

I then open the zip file with Winrar on my windows machine and in the comment you can see \r\n , \t appearing as is ... indicating that either winrar don't allow this or i wrong set the zipArchive comment.


Solution

  • I cannot reproduce your problem based on your simplified demo. WinRAR displays it as expected. But I can mimic the result you get by using single quotes for the comment string.

    <?php
    $zip = new ZipArchive;
    $res = $zip->open('test.zip', ZipArchive::CREATE);
    if ($res === TRUE) {
        $zip->addFromString('test.txt', 'file content goes here');
    
        $zip->setArchiveComment("File \t Stats\r\nSecond line ... some other text \r\n"); // OK
        $comment = "File \t Stats\r\n";
        $comment .= 'Second line ... some other text \r\n';
        $zip->setArchiveComment($comment); // NOT OK for the second line
    
        $zip->close();
        echo 'ok';
    } else {
        echo 'failed';
    }