压缩文件并使用 PHP 中的密码进行保护

ZIP a file and protect with a password in PHP

我有这个压缩文件的代码,但我需要用密码保护这个文件

$file = 'backup.sql';
$zipname = $file.'.zip';
$zip = new ZipArchive;
$zip->open($zipname, ZipArchive::CREATE);
ZipArchive::setPassword('123456');
//$zip->setPassword("123456");
$zip->addFile($file);
$zip->close();

当我使用 $zip->setPassword 时,我没有收到任何错误,但文件根本没有受到保护,当我使用 ZipArchive::setPassword 时,我收到此错误 "Fatal error: Non-static method ZipArchive::setPassword() cannot be called statically"

那么如何压缩 php 中的文件并用密码保护它?

ZipArchive::setPassword 此功能仅设置用于解压缩存档的密码;它不会将非密码保护的 ZipArchive 变成密码保护的 ZipArchive。

工作代码:

$文件='file_name_to_be_compressed.extension'

system('zip -P ZIP_PASSWORD '.$file.'.zip'.$file);

documentation 中所述:

This function only sets the password to be used to decompress the archive; it does not turn a non-password-protected ZipArchive into a password-protected ZipArchive.

如果您想加密 zip 存档,我建议 google 一点 :)

是的,不支持创建受密码保护的存档(它们将被简单地创建为不受保护的存档)。
但是,它仍然可以用来提取受密码保护的档案。

回到问题中
你总是可以

<?php echo system('zip -P pass file.zip file.txt'); ?>

(这对 Windows 和我们心爱的 Linux 都有效)

但是,如果它不符合您的要求,让我们继续。
我建议您使用 DotNetZip(仅限 Windows),您将从 PHP.

准确地动态生成 AES 加密的 zip 存档
<?php
// origin: 
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。

So,虽然PHP是一门成熟的语言,但没有足够的方法(不包括自定义扩展或类似的东西)来用纯PHP.
您还可以做的是等到 PHP 7.2 will be available for production (cuz ZipArchive::setEncryptionName 实现(感谢 Pierre 和 Remi))。
但是,在那之前你也可以尝试将 php_zip >= 1.14.0 移植到 PHP < 7.2,但是目前没有可用的编译二进制文件,所以你必须自己编译它并尝试它是否是完全有可能(我相信是)。
p.s。我想试试,但我的电脑上现在没有 VS2015+。

从 PHP 7.2 开始,您可以使用 setEncryptionName 创建受密码保护的 ZIP 存档。

使用PHP 7.2 创建受密码保护的 zip 文件:

$zip = new ZipArchive;
$res = $zip->open('filename.zip', ZipArchive::CREATE); //Add your file name
if ($res === TRUE) {
   $zip->addFromString('FILENAME_WITH_EXTENSION', 'file content goes here'); //Add your file name
   $zip->setEncryptionName('FILENAME_WITH_EXTENSION', ZipArchive::EM_AES_256, 'PASSWORD'); //Add file name and password dynamically
   $zip->close();
   echo 'ok';
} else {
   echo 'failed';
}