You are here

function hackedProjectWebFilesDownloader::archive_extract in Hacked! 8.2

Unpack a downloaded archive file.

Parameters

string $file: The filename of the archive you wish to extract.

string $directory: The directory you wish to extract the archive into.

Return value

Archiver The Archiver object used to extract the archive.

Throws

Exception on failure.

1 call to hackedProjectWebFilesDownloader::archive_extract()
hackedProjectWebFilesDownloader::download in src/hackedProjectWebFilesDownloader.php
Download the remote files to the local filesystem.

File

src/hackedProjectWebFilesDownloader.php, line 109

Class

hackedProjectWebFilesDownloader
Downloads a project using a standard Drupal method.

Namespace

Drupal\hacked

Code

function archive_extract($file, $directory) {
  $archiver = \Drupal::service('plugin.manager.archiver')
    ->getInstance([
    'filepath' => $file,
  ]);
  if (!$archiver) {
    throw new Exception(t('Cannot extract %file, not a valid archive.', [
      '%file' => $file,
    ]));
  }

  // Remove the directory if it exists, otherwise it might contain a mixture of
  // old files mixed with the new files (e.g. in cases where files were removed
  // from a later release).
  $files = $archiver
    ->listContents();

  // Unfortunately, we can only use the directory name for this. :(
  $project = mb_substr($files[0], 0, -1);
  $extract_location = $directory . '/' . $project;
  if (file_exists($extract_location)) {
    \Drupal::service('file_system')
      ->deleteRecursive($extract_location);
  }
  $archiver
    ->extract($directory);
  return $archiver;
}