容易挤柠檬(不)。
是的,不支持创建受密码保护的存档(正如您刚刚描述的那样,它们将被创建为不受保护的存档)。
但是,它仍然可以用于提取受密码保护的档案。
回到问题。
你总是可以
<?php echo system('zip -P pass file.zip file.txt'); ?>
(这将同时在Windows和我们钟爱的Linux上运行)
但是,如果它不符合您的要求,让我们继续。
我建议您使用DotNetZip(仅限Windows),您将完全从PHP动态生成AES加密的zip存档。
<?php
// origin: https://stackoverflow.com/a/670804/3684575
try
{
$fname = "zip-generated-from-php-" . date('Y-m-d-His') . ".zip";
$zipOutput = "c:\\temp\\" . $fname;
$zipfact = new COM("Ionic.Zip.ZipFile");
$zip->Name = $zipOutput;
$dirToZip= "c:\\temp\\psh";
# Encryption: 3 => 256-bit AES.
# 2 => 128-bit AES.
# 1 => PKZIP (Weak).
# 0 => None
$zip->Encryption = 3;
$zip->Password = "AES-Encryption-Is-Secure";
$zip->AddDirectory($dirToZip);
$zip->Save();
$zip->Dispose();
if (file_exists($zipOutput))
{
header('Cache-Control: no-cache, must-revalidate');
header('Content-Type: application/x-zip');
header('Content-Disposition: attachment; filename=' . $fname);
header('Content-Length: ' . filesize($zipOutput));
readfile($zipOutput);
unlink($zipOutput);
}
else
{
echo '<html>';
echo ' <head>';
echo ' <title>Calling DotNetZip from PHP through COM</title>';
echo ' <link rel="stylesheet" href="basic.css"/>';
echo ' </head>';
echo '<body>';
echo '<h2>Whoops!</h2>' . "<br/>\n";
echo '<p>The file was not successfully generated.</p>';
echo '</body>';
echo '</html>';
}
}
catch (Exception $e)
{
echo '<html>';
echo ' <head>';
echo ' <title>Calling DotNetZip from PHP through COM</title>';
echo ' <link rel="stylesheet" href="basic.css"/>';
echo ' </head>';
echo '<body>';
echo '<h2>Whoops!</h2>' . "<br/>\n";
echo '<p>The file was not successfully generated.</p>';
echo '<p>Caught exception: ', $e->getMessage(), '</p>', "\n";
echo '<pre>';
echo $e->getTraceAsString(), "\n";
echo '</pre>';
echo '</body>';
echo '</html>';
}
?>
但是,这仍然是一个非常肮脏的解决方案,而更多的解决方案无法在Linux上运行。
因此,尽管PHP是一种成熟的语言,但是没有足够的方法(不包括自定义扩展名或类似的扩展名)来使用纯PHP实现如此简单的任务。
您还可以做的是,等到PHP 7.2投入生产为止(因为实现了ZipArchive :: setEncryptionName,这要感谢Pierre和Remi)。
但是,在那之前,您还可以尝试将php_zip > = 1.14.0移植到PHP <7.2,但是目前没有可用的已编译二进制文件,因此您必须自己进行编译并尝试一下是否有可能(我相信这是)。
ps我会尝试的,但是我的PC上现在没有VS2015 +。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句