You are here

public static function TincanService::unzipPackage in Opigno module 3.x

Same name and namespace in other branches
  1. 8 ActivityTypes/opigno_tincan_activity/src/TincanService.php \Drupal\opigno_tincan_activity\TincanService::unzipPackage()

This method will unzip the package to the public extracted folder.

It will use the constants self::PATH_PUBLIC_EXTRACTED_PACKAGE_FOLDER.

Parameters

object $file: The file to unzip.

Return value

bool TRUE if success, else FALSE.

Throws

\Exception If the file is unsupported.

1 call to TincanService::unzipPackage()
TincanService::saveTincanPackageInfo in ActivityTypes/opigno_tincan_activity/src/TincanService.php
Save tincan package.

File

ActivityTypes/opigno_tincan_activity/src/TincanService.php, line 185

Class

TincanService
Class TincanService.

Namespace

Drupal\opigno_tincan_activity

Code

public static function unzipPackage($file) {
  $path = \Drupal::service('file_system')
    ->realpath($file
    ->getFileUri());
  $zip = new \ZipArchive();
  $result = $zip
    ->open($path);
  if ($result === TRUE) {
    $extract_dir = self::getExtractPath($file);
    $zip
      ->extractTo($extract_dir);
    $zip
      ->close();
  }
  else {
    $error = 'none';
    switch ($result) {
      case \ZipArchive::ER_EXISTS:
        $error = 'ER_EXISTS';
        break;
      case \ZipArchive::ER_INCONS:
        $error = 'ER_INCONS';
        break;
      case \ZipArchive::ER_INVAL:
        $error = 'ER_INVAL';
        break;
      case \ZipArchive::ER_NOENT:
        $error = 'ER_NOENT';
        break;
      case \ZipArchive::ER_NOZIP:
        $error = 'ER_NOZIP';
        break;
      case \ZipArchive::ER_OPEN:
        $error = 'ER_OPEN';
        break;
      case \ZipArchive::ER_READ:
        $error = 'ER_READ';
        break;
      case \ZipArchive::ER_SEEK:
        $error = 'ER_SEEK';
        break;
    }
    \Drupal::logger('opigno_tincan_activity')
      ->error("An error occurred when unzipping the TINCAN package data. Error: !error", [
      '!error' => $error,
    ]);
    return FALSE;
  }
}