新的"setPassword"方法不生效(除非我误解了)。
这是我的示例代码:
<?php
$zipFilePath = sys_get_temp_dir() . DIRECTORY_SEPARATOR . 'filename.zip';
$password = 'P455W0RD';
if (file_exists($zipFilePath)) {
unlink($zipFilePath);
}
$zipArchive = new ZipArchive();
$zipArchive->open($zipFilePath, ZipArchive::CREATE);
if ($zipArchive->setPassword($password)) {
echo 'OK' . PHP_EOL;
}
foreach (range(1, 10) as $fileNumber) {
$zipArchive->addFromString('file' . $fileNumber . '.txt', rand());
}
$zipArchive->close();
在PHP 5.6.0beta3 (Debian Testing)中打印"OK",但是没有密码
我错过了什么?
我找到了一种绕过ZipArchive::setPassword()方法的方法。我只是写了一个shell脚本:
#!/bin/bash
command -v zipcloak && echo "exist" || exit -1;
command -v expect && echo "exist" || exit -1;
MYPWD="[password]"
expect -c '
spawn zipcloak [filename]
expect "*Enter password*"
sleep 0.1
send "'"$MYPWD"''r"
sleep 0.1
expect "*Verify password*"
sleep 0.1
send "'"$MYPWD"''r"
sleep 0.1
'
我可以简单地使用exec从我的php代码:
public function encryptZip($filename, $password, $bashdir){
$bash = str_replace('[filename]', $filename, (str_replace('[password]', $password, file_get_contents($bashdir))));
exec($bash);
}