WP_CLI
Extractor::extract_tarball
Extract a tarball to a specific destination.
Method of the class: Extractor{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = Extractor::extract_tarball( $tarball, $dest );
- $tarball(string) (required)
- .
- $dest(string) (required)
- .
Extractor::extract_tarball() Extractor::extract tarball code WP-CLI 2.13.0-alpha
private static function extract_tarball( $tarball, $dest ) {
// Ensure the destination folder exists or can be created.
if ( ! self::ensure_dir_exists( $dest ) ) {
throw new Exception( "Could not create folder '{$dest}'." );
}
if ( class_exists( 'PharData' ) ) {
try {
$phar = new PharData( $tarball );
$name = Utils\basename( $tarball );
$tempdir = Utils\get_temp_dir()
. uniqid( 'wp-cli-extract-tarball-', true )
. "-{$name}";
$phar->extractTo( $tempdir );
self::copy_overwrite_files(
self::get_first_subfolder( $tempdir ),
$dest
);
self::rmdir( $tempdir );
return;
} catch ( Exception $e ) {
WP_CLI::warning(
"PharData failed, falling back to 'tar xz' ("
. $e->getMessage() . ')'
);
// Fall through to trying `tar xz` below.
}
}
// Ensure relative paths cannot be misinterpreted as hostnames.
// Prepending `./` will force tar to interpret it as a filesystem path.
if ( self::path_is_relative( $tarball ) ) {
$tarball = "./{$tarball}";
}
if ( ! file_exists( $tarball )
|| ! is_readable( $tarball )
|| filesize( $tarball ) <= 0 ) {
throw new Exception( "Invalid zip file '{$tarball}'." );
}
// Note: directory must exist for tar --directory to work.
$cmd = Utils\esc_cmd(
'tar xz --strip-components=1 --directory=%s -f %s',
$dest,
$tarball
);
$process_run = WP_CLI::launch(
$cmd,
false, /*exit_on_error*/
true /*return_detailed*/
);
if ( 0 !== $process_run->return_code ) {
throw new Exception(
sprintf(
'Failed to execute `%s`: %s.',
$cmd,
self::tar_error_msg( $process_run )
)
);
}
}