function update_manager_archive_extract

Same name in other branches
  1. 7.x modules/update/update.manager.inc \update_manager_archive_extract()
  2. 9 core/modules/update/update.manager.inc \update_manager_archive_extract()
  3. 8.9.x core/modules/update/update.manager.inc \update_manager_archive_extract()
  4. 11.x core/modules/update/update.manager.inc \update_manager_archive_extract()

Unpacks 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

\Drupal\Core\Archiver\ArchiverInterface The Archiver object used to extract the archive.

Throws

Exception

2 calls to update_manager_archive_extract()
UpdateManagerInstall::submitForm in core/modules/update/src/Form/UpdateManagerInstall.php
Form submission handler.
update_manager_batch_project_get in core/modules/update/update.manager.inc
Implements callback_batch_operation().

File

core/modules/update/update.manager.inc, line 155

Code

function update_manager_archive_extract($file, $directory) {
    
    /** @var \Drupal\Core\Archiver\ArchiverInterface $archiver */
    $archiver = \Drupal::service('plugin.manager.archiver')->getInstance([
        'filepath' => $file,
    ]);
    if (!$archiver) {
        throw new Exception("Cannot extract '{$file}', not a valid archive");
    }
    // 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 to determine the project
    // name. Some archivers list the first file as the directory (i.e., MODULE/)
    // and others list an actual file (i.e., MODULE/README.TXT).
    $project = strtok($files[0], '/\\');
    $extract_location = $directory . '/' . $project;
    if (file_exists($extract_location)) {
        try {
            \Drupal::service('file_system')->deleteRecursive($extract_location);
        } catch (FileException $e) {
            // Ignore failed deletes.
        }
    }
    $archiver->extract($directory);
    return $archiver;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.