PharData::extractTo

(PHP 5 >= 5.3.0, PHP 7, PHP 8, PECL phar >= 2.0.0)

PharData::extractToExtraer el contenido de un archivo tar/zip a un directorio

Descripción

PharData::extractTo(string $pathto, string|array $files = ?, bool $overwrite = false): bool

Extrae todos los ficheros de un archivo tar/zip al disco. Los ficheros y directorios extraídos conservan los mismos permisos que los almacenados en el archivo. Los parámetros opcionales permiten controlar qué ficheros serán extraídos, y si los ficheros existentes en disco podrán ser sobrescritos. El segundo parámetro files puede ser el nombre de un fichero o directorio a extraer, o un array de nombre de ficheros y directorios a extraer. Por omisión, este método no sobrescribirá los ficheros existentes, aunque el tercer parámetro se puede establecer a true para habilitar la sobrescritura de ficheros. Este método es similar a ZipArchive::extractTo().

Parámetros

pathto

Ruta donde extraer los ficheros dados por files

files

El nombre de un fichero o directorio a extraer, o un array de ficheros/directorios a extraer.

overwrite

Esteblecer a true para habilitar la sobrescritura de ficheros existentes

Valores devueltos

Devuelve true en caso de éxito, pero es mejor comprobar si lanza alguna excepción, y asumir el éxito si no se lanza ninguna.

Errores/Excepciones

Lanza una excepción de tipo PharException si ocurrió algún error al volcar los cambios al disco.

Ejemplos

Ejemplo #1 Un ejemplo de PharData::extractTo()

<?php
try {
$phar = new PharData('miphar.tar');
$phar->extractTo('/ruta/completa'); // extraer todos los ficheros
$phar->extractTo('/otra/ruta', 'fichero.txt'); // extraer solamente fichero.txt
$phar->extractTo('/esta/ruta',
array(
'fichero1.txt', 'fichero2.txt')); // extraer solamente 2 ficheros
$phar->extractTo('/tercera/ruta', null, true); // extraer todos los ficheros y sobrescribirlos
} catch (Exception $e) {
// manejar errores
}
?>

Ver también

add a note add a note

User Contributed Notes 2 notes

up
2
Anonymous
6 years ago
I'm unable to extract the first directory from a tar archive:
the destination dir remains empty,
no error is thrown

<?php
$tar
= new \PharData('archive.tar');
if (
$tar->current()->isDir()) {
                echo
'is_dir';
               
$dir = $tar->current()->getPathname();
               
$dir = basename($dir);
               
$tar->extractTo('destination', $dir);
}
?>

the docs hint that the second param could be a name of file OR DIR to be extracted from the archive, is that really possible?
up
1
njh at aelius dot com
11 years ago
Note that PHAR only supports extracting the 'ustar' variant of the tar archives.

Some systems (such as older versions of Mac OS X) generate the 'pax' format by default.

See here for more information:
http://php.net/manual/pl/phar.fileformat.tar.php
To Top