0

I have created zip with encryption using setEncryptionName, as follows:

if($zip->open($zip_destination_real,\ZipArchive::CREATE) === TRUE) 
{
  $zip->addFile($filePath_real,'xyz.txt');   
  $zip->setEncryptionName('xyz.txt', \ZipArchive::EM_AES_256, '12345');         
  $zip->close();

}

Now, how to extract this zip file? extractTo function is returning false.

$r = $zip->extractTo($dir_real); var_dump($r);

I use php 7.2

Even when I manually extract the folder it asks for password.I enter 12345 as set, but error pops up , saying Error occured while extracting files.

user17469
  • 3
  • 2

1 Answers1

0

You didn't set password correctly.

Zip files with password:

# Creating new zip object
$zip = new ZipArchive();
if ($zip->open('file.zip', ZipArchive::CREATE) === TRUE) {

    # Setting password here
    $zip->setPassword('12345');

    # Adding some files to zip
    $zip->addFile('some-file.txt');
    $zip->setEncryptionName('some-file.txt', ZipArchive::EM_AES_256);

    # Closing instance of zip object
    $zip->close();

    exit("Done! Your zip is ready!")
} else {
    exit("Whoops:( Failed to create zip.");
}

And unzip like this:

# Creating new ZipArchive instance
$zip = new ZipArchive();

# Open file to read
if ($zip->open('file.zip') === true) {

    # Enter your password
    $zip->setPassword('12345');

    # Extract files to some destination
    # dirname(__FILE__) sets destination to directory of current file
    $zip->extractTo(dirname(__FILE__));

    # Closing instance of zip object
    $zip->close();
}