protected function unzip($zip, $path)
{
// build the temporary folder path
$tmp = $this->tmp(preg_replace('!.zip$!', '', $zip));
// extract the zip file
util::unzip($zip, $tmp);
// get the list of directories within our tmp folder
$dirs = glob($tmp . '/*');
// get the source directory from the tmp folder
if (isset($dirs[0]) && is_dir($dirs[0])) {
$source = $dirs[0];
} else {
throw new RuntimeException('The source directory could not be found');
}
// create the folder if it does not exist yet
if (!is_dir($path)) {
mkdir($path);
}
// extract the content of the directory to the final path
foreach ((array) array_diff(scandir($source), ['.', '..']) as $name) {
if (!rename($source . '/' . $name, $path . '/' . $name)) {
throw new RuntimeException($name . ' could not be copied');
}
}
// remove the zip file
util::remove($zip);
// remove the temporary folder
util::remove($tmp);
}