示例#1
0
 /**
  * @param string      $container
  * @param string      $path
  * @param \ZipArchive $zip
  * @param string      $zipFileName
  * @param bool        $overwrite
  *
  * @throws \Exception
  * @throws BadRequestException
  * @return string Zip File Name created/updated
  */
 public function getFolderAsZip($container, $path, $zip = null, $zipFileName = '', $overwrite = false)
 {
     $root = static::addContainerToName($container, '');
     if (!is_dir($root)) {
         throw new BadRequestException("Can not find directory '{$root}'.");
     }
     $needClose = false;
     if (!isset($zip)) {
         $needClose = true;
         $zip = new \ZipArchive();
         if (empty($zipFileName)) {
             $temp = basename($path);
             if (empty($temp)) {
                 $temp = $container;
             }
             $tempDir = rtrim(sys_get_temp_dir(), DIRECTORY_SEPARATOR) . DIRECTORY_SEPARATOR;
             $zipFileName = $tempDir . $temp . '.zip';
         }
         if (true !== $zip->open($zipFileName, $overwrite ? \ZipArchive::OVERWRITE : \ZipArchive::CREATE)) {
             throw new InternalServerErrorException("Can not create zip file for directory '{$path}'.");
         }
     }
     FileUtilities::addTreeToZip($zip, $root, rtrim($path, '/'));
     if ($needClose) {
         $zip->close();
     }
     return $zipFileName;
 }