/** * @param string $container * @param string $path * @param \ZipArchive $zip * @param bool $clean * @param string $drop_path * * @throws \Exception * @return array */ public function extractZipFile($container, $path, $zip, $clean = false, $drop_path = '') { if ($clean) { try { // clear out anything in this directory $dir = static::addContainerToName($container, $path); FileUtilities::deleteTree($dir, true, false); } catch (\Exception $ex) { throw new InternalServerErrorException("Could not clean out existing directory {$path}.\n{$ex->getMessage()}"); } } for ($i = 0; $i < $zip->numFiles; $i++) { $name = $zip->getNameIndex($i); if (empty($name)) { continue; } if (!empty($drop_path)) { $name = str_ireplace($drop_path, '', $name); } $fullPathName = $path . $name; if ('/' === substr($fullPathName, -1)) { $this->createFolder($container, $fullPathName, true, [], false); } else { $parent = FileUtilities::getParentFolder($fullPathName); if (!empty($parent)) { $this->createFolder($container, $parent, true, [], false); } $content = $zip->getFromIndex($i); $this->writeFile($container, $fullPathName, $content); } } return ['name' => rtrim($path, DIRECTORY_SEPARATOR), 'path' => $path, 'type' => 'file']; }
/** * @param string $container * @param string $path * @param \ZipArchive $zip * @param bool $clean * @param string $drop_path * * @return array * @throws \Exception */ public function extractZipFile($container, $path, $zip, $clean = false, $drop_path = '') { if ($clean) { try { // clear out anything in this directory $blobs = $this->listBlobs($container, $path); if (!empty($blobs)) { foreach ($blobs as $blob) { if (0 !== strcasecmp($path, $blob['name'])) { // not folder itself $this->deleteBlob($container, $blob['name']); } } } } catch (\Exception $ex) { throw new InternalServerErrorException("Could not clean out existing directory {$path}.\n{$ex->getMessage()}"); } } for ($i = 0; $i < $zip->numFiles; $i++) { try { $name = $zip->getNameIndex($i); if (empty($name)) { continue; } if (!empty($drop_path)) { $name = str_ireplace($drop_path, '', $name); } $fullPathName = $path . $name; if ('/' === substr($fullPathName, -1)) { $this->createFolder($container, $fullPathName, true, [], false); } else { $parent = FileUtilities::getParentFolder($fullPathName); if (!empty($parent)) { $this->createFolder($container, $parent, true, [], false); } $content = $zip->getFromIndex($i); $this->writeFile($container, $fullPathName, $content); } } catch (\Exception $ex) { throw $ex; } } return ['name' => rtrim($path, DIRECTORY_SEPARATOR), 'path' => $path]; }