How to use ZipArchive class to extract zip file in php

Here are the question is, we can use php ZipArchive class to extact any zip file by code in your user defined directory.

1) The function which perform this functionality is ,

function extractZip($sourceFile, $Targetpath)  {
$zipObject = new ZipArchive();
$flag = $zipObject->open($sourceFile);
if($flag === true) {
$zipObject->extractTo($Targetpath);
$zipObject->close();
} else {
die(“There was a problem. Please try again!”);
}
}

2) The above function can use to extract zip file by call , extractZip(‘SourceFile’, ‘TargetFolder’);

3) extractZip function use the main ZipArchive class file function like ZipArchive::open(), ZipArchive::extractTo(), ZipArchive::close().

4) Remember, to use this function, ;extension=php_zip.dll enable in php.ini, and also enable zip enabled and install in your server.

5) May be there, zip not installed in your server, so if you want to install that you have to get root login in terminal in ssh or you can use putty ,

then hit the command -> pear install zip-1.10.2.tgz