如果文件使用 setEncryptionName 加密,如何在 PHP 中提取 zip 存档
How to extract zip archive in PHP if file encrypted using setEncryptionName
我已经使用 setEncryptionName 创建了加密的 zip,如下所示:
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();
}
现在,如何解压这个 zip 文件?
extractTo 函数返回 false。
$r = $zip->extractTo($dir_real);
var_dump($r);
我用php7.2
即使我手动提取文件夹时它要求 password.I 输入 12345 作为设置,但弹出错误,说提取文件时发生错误。
您没有正确设置密码。
带密码的 Zip 文件:
# 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.");
}
然后像这样解压:
# 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();
}
我已经使用 setEncryptionName 创建了加密的 zip,如下所示:
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();
}
现在,如何解压这个 zip 文件? extractTo 函数返回 false。
$r = $zip->extractTo($dir_real); var_dump($r);
我用php7.2
即使我手动提取文件夹时它要求 password.I 输入 12345 作为设置,但弹出错误,说提取文件时发生错误。
您没有正确设置密码。
带密码的 Zip 文件:
# 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.");
}
然后像这样解压:
# 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();
}