WP_CLI

Extractor::extract_zip()private staticWP-CLI 1.0

Extract a ZIP file to a specific destination.

Method of the class: Extractor{}

No Hooks.

Return

null. Nothing.

Usage

$result = Extractor::extract_zip( $zipfile, $dest );
$zipfile(string) (required)
-
$dest(string) (required)
-

Extractor::extract_zip() code WP-CLI 2.8.0-alpha

private static function extract_zip( $zipfile, $dest ) {
	if ( ! class_exists( 'ZipArchive' ) ) {
		throw new Exception( 'Extracting a zip file requires ZipArchive.' );
	}

	// Ensure the destination folder exists or can be created.
	if ( ! self::ensure_dir_exists( $dest ) ) {
		throw new Exception( "Could not create folder '{$dest}'." );
	}

	if ( ! file( $zipfile )
		|| ! is_readable( $zipfile )
		|| filesize( $zipfile ) <= 0 ) {
		throw new Exception( "Invalid zip file '{$zipfile}'." );
	}

	$zip = new ZipArchive();
	$res = $zip->open( $zipfile );

	if ( true === $res ) {
		$name    = Utils\basename( $zipfile );
		$tempdir = Utils\get_temp_dir()
					. uniqid( 'wp-cli-extract-zipfile-', true )
					. "-{$name}";

		$zip->extractTo( $tempdir );
		$zip->close();

		self::copy_overwrite_files(
			self::get_first_subfolder( $tempdir ),
			$dest
		);

		self::rmdir( $tempdir );
	} else {
		throw new Exception(
			sprintf(
				"ZipArchive failed to unzip '%s': %s.",
				$zipfile,
				self::zip_error_msg( $res )
			)
		);
	}
}