function drush_tarball_extract

8.0.x drush.inc drush_tarball_extract($path, $destination = FALSE, $listing = FALSE, $tar_extra_options = '')
6.x drush.inc drush_tarball_extract($path, $destination = FALSE, $listing = FALSE, $tar_extra_options = '')
7.x drush.inc drush_tarball_extract($path, $destination = FALSE, $listing = FALSE, $tar_extra_options = '')
4.x drush.inc drush_tarball_extract($path, $destination = FALSE, $listing = FALSE)
5.x drush.inc drush_tarball_extract($path, $destination = FALSE, $listing = FALSE)
master drush.inc drush_tarball_extract($path, $destination = FALSE, $listing = FALSE, $tar_extra_options = '')

Extract a tarball.

Parameters

string $path: The name of the .tar.gz or .tgz file to be extracted.

string $destination: The destination directory the tarball should be extracted into. Optional, if ommitted the tarball directory will be used as destination.

boolean $listing: If TRUE, a listing of the tar contents will be returned on success.

Return value

string TRUE on success, FALSE on fail. If $listing is TRUE, a file listing of the tarball is returned if the extraction reported success, instead of TRUE.

Related topics

2 calls to drush_tarball_extract()
drush_archive_restore in commands/core/archive.drush.inc
Command callback. Restore web site(s) from a site archive file.
package_handler_download_project in commands/pm/package_handler/wget.inc
Download a project.

File

includes/drush.inc, line 1560
The drush API implementation and helpers.

Code

function drush_tarball_extract($path, $destination = FALSE, $listing = FALSE) {
  if (!file_exists($path)) {
    return drush_set_error('TARBALL_EXTRACT_NOT_FOUND', dt('Tarball !path could not be found.', array('!path' => $path)));
  }
  $olddir = getcwd();
  if (!$destination) {
    $destination = dirname($path);
  }
  if (!is_writeable($destination)) {
    return drush_set_error('TARBALL_EXTRACT_DESTINATION', dt('Extracting !path failed, as the destination directory !dest was not found or could not be written to.', array('!path' => $path, '!dest' => $dest)));
  }
  // If we are not on Windows, then try to do "tar" in a single operation.
  if ((!drush_is_windows()) && (drush_shell_cd_and_exec(dirname($path), "tar -C %s -xzf %s", $destination, basename($path)))) {
    if ($listing) {
      // We use a separate tar -tzf instead of -xvf above because
      // the output is not the same in Mac.
      drush_shell_cd_and_exec(dirname($path), "tar -tzf %s", basename($path));
      return drush_shell_exec_output();
    }
    return TRUE;
  }
  // If we could not get the single-op tar to work, do it in three steps.
  // Copy the source tarball to the destination directory.  Rename to a temp name in case the destination directory == dirname($path)
  $paths_basename = basename(basename($path, '.tar.gz'), '.tgz');
  $tarball = drush_tempnam($paths_basename, $destination) . ".tar.gz";
  drush_register_file_for_deletion($tarball);
  drush_copy_dir($path, $tarball);
  $unzipped = $destination . '/' . basename($tarball, ".tar.gz") . ".tar";
  // We used to use gzip --decompress in --stdout > out, but the output redirection sometimes failed on Windows for some binary output
  drush_shell_cd_and_exec(dirname($tarball), "gzip --decompress %s", $tarball);
  if (file_exists($unzipped)) {
    drush_register_file_for_deletion($unzipped);
    if (drush_shell_cd_and_exec(dirname($unzipped), "tar -xf %s", basename($unzipped))) {
      if ($listing) {
        // We use a separate tar -tf instead of -xf above because
        // the output is not the same in Mac.
        drush_shell_cd_and_exec(dirname($unzipped), "tar -tf %s", basename($unzipped));
        return drush_shell_exec_output();
      }
      return TRUE;
    }
    return drush_set_error('TARBALL_EXTRACT_TAR_FAIL', dt('Extracting !path using the tar command failed.', array('!path' => $path)));
  }
  else {
    return drush_set_error('TARBALL_EXTRACT_GZIP_FAIL', dt('Uncompressing !path using  the gzip command failed.', array('!path' => $path)));
  }
}