How to extract a ZIP file that has a password usin

2019-01-26 16:52发布

问题:

I have seen only one question on here but it does not answer my question. I am running a typical LAMP server that has the most up to date PHP 5 and MYSQL 5 with Redhat Linux.

I need to find a PHP only solution because my host does not allow me to use shell.

Here is my code that extracts ZIPs that are not passworded from vBulletin uploads to another directory:

if ($_GET['add'] == TRUE){
$zip = new ZipArchive;
 $res = $zip->open($SOURCE FOLDER);
 if ($res === TRUE) {
     $zip->extractTo('$DESTINATION FOLDER/');
     $zip->close();
     echo 'File has been added to the library successfuly';
     //Add a flag to that file to indicate it has already been added to the library.
     mysql_query("UPDATE attachment SET library = 1 WHERE filedataid='$fileid'");    
 } else {
     echo 'A uncompression or file error has occured';
 }}

There must be some way to do this using just PHP, surely! Thank you.

UPDATE: My host informs me that gzip is installed on the server but not 7-Zip. I am looking into shell access too.

回答1:

Have 7zip executable available to the script and call it to uncompress the file with the password via system().

$strCommandLine = "7z e fileToUnzip.7z -pTHEPASSWORD";
system($strCommandLine);

You can do something similar with unzip, if you host has that installed. See http://linux.about.com/od/commands/l/blcmdl1_unzip.htm.

It supports -P with a password, so something like this:

$strCommandLine = "unzip fileToUnzip.7z -P THEPASSWORD";
system($strCommandLine);

Caveat: someone could see your password on that command line if they do a ps on the system and see your unzip command running.



回答2:

you can use this function setPassword ( string $password ) http://php.net/manual/en/ziparchive.setpassword.php