Skip to content

ZipArchive password protection doesn't work if entryname is used in addFile() #9318

@zoltan-sule

Description

@zoltan-sule

Description

The following code:

<?php
$zip = new ZipArchive();

if ($zip->open('/tmp/test1.zip', ZipArchive::CREATE) === TRUE) {
    $zip->setPassword('secret_');
    $zip->addFile('/tmp/test.txt', 'test.txt');
    $zip->setEncryptionName('/tmp/test.txt', ZipArchive::EM_AES_256);
    $zip->close();
}

Resulted in this output:
I am able to extract the content without the password.

But I expected this output instead:
that I need to use the password during the extraction.

Working but not optimal
If I leave entryname empty in addFile() method then I need to type the password before I want to extract the content but then the tmp appears inside the zip file as a directory structure.

<?php
$zip = new ZipArchive();

if ($zip->open('/tmp/test2.zip', ZipArchive::CREATE) === TRUE) {
    $zip->setPassword('secret_');
    $zip->addFile('/tmp/test.txt');
    $zip->setEncryptionName('/tmp/test.txt', ZipArchive::EM_AES_256);
    $zip->close();
}

PHP Version

PHP 8.1.8

Operating System

Ubuntu 20.04.4 LTS

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions