Php 程序打开ZipClok不';行不通
我在为zip文件设置密码时遇到问题。服务器运行PHP5.5,只有PHP5.6才支持Php 程序打开ZipClok不';行不通,php,shell,zip,ziparchive,Php,Shell,Zip,Ziparchive,我在为zip文件设置密码时遇到问题。服务器运行PHP5.5,只有PHP5.6才支持ZipArchive::setPassword()。我的老板还不想升级到PHP5.6,所以我不得不通过使用proc_open()和zipcloak命令绕过自动生成的zip文件的密码设置。然而,它似乎不起作用。这是我的密码: /** * filename = the name of the zip file you want to encrypt containing the file path a
ZipArchive::setPassword()。我的老板还不想升级到PHP5.6,所以我不得不通过使用proc_open()
和zipcloak
命令绕过自动生成的zip文件的密码设置。然而,它似乎不起作用。这是我的密码:
/**
* filename = the name of the zip file you want to encrypt containing the file path as well
**/
public function encryptZip($filename, $password){
$command = 'zipcloak ' . $filename;
$descriptorspec = array(
0 => array("pipe", "r"), // stdin
1 => array("pipe", "w"), // stdout
2 => array("pipe", "w"), // stderr
);
// Opening the process
$process = proc_open($command, $descriptorspec, $pipes);
if(is_resource($process))
{
fwrite($pipes[0], $password."\n".$password);
fclose($pipes[0]);
fclose($pipes[1]);
fclose($pipes[2]);
// Closing the process
$return_value = proc_close($process); // This prints 12
}
}
}
// end of class
$zip = new DZip();
$zip->encryptZip('path/to/zip.zip', '12345');
zipcloak命令要求输入两次密码,这就是为什么我使用fwrite($pipes[0],$password.\n“$password)代码>。我在网上搜索了几个小时寻找更简单的解决方案,我也找到了zip-P[password]
,但它只创建新文件,我使用ZipArchive
创建我的zip文件,因为zip的文件夹结构需要它。有什么帮助吗?我没有收到任何错误消息。提前谢谢 所以,我自己找到了解决办法。我创建了一个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
'
我可以从php代码中简单地使用exec:
public function encryptZip($filename, $password, $bashdir){
$bash = str_replace('[filename]', $filename, (str_replace('[password]', $password, file_get_contents($bashdir))));
exec($bash);
}
它只在安装了expect
和zipcloak
的linux服务器上工作,但这对我来说不是问题。我们运行linux并安装了这两个工具。另一种选择是:编译zip-3.0源代码并使用未记录的
-DPASSWD_FROM_STDIN
unix/Makefile中的选项。这将允许您从stdin输入密码(重复两次)
CFLAGS_NOOPT = -I. -DUNIX $(LOCAL_ZIP) -DPASSWD_FROM_STDIN
你是不是碰巧上了Windows?