src/Tar.php
public function extract($outdir, $strip = '', $exclude = '', $include = '')
    {
        if ($this->closed || !$this->file) {
            throw new ArchiveIOException('Can not read from a closed archive');
        }

        $outdir = rtrim($outdir, '/');
        @mkdir($outdir, 0777, true);
        if (!is_dir($outdir)) {
            throw new ArchiveIOException("Could not create directory '$outdir'");
        }

        $extracted = array();
        while ($dat = $this->readbytes(512)) {
            // read the file header
            $header = $this->parseHeader($dat);
            if (!is_array($header)) {
                continue;
            }
            $fileinfo = $this->header2fileinfo($header);

            // apply strip rules
            $fileinfo->strip($strip);

            // skip unwanted files
            if (!strlen($fileinfo->getPath()) || !$fileinfo->match($include, $exclude)) {
                $this->skipbytes(ceil($header['size'] / 512) * 512);
                continue;
            }

            // create output directory
            $output    = $outdir.'/'.$fileinfo->getPath();
            $directory = ($fileinfo->getIsdir()) ? $output : dirname($output);
            @mkdir($directory, 0777, true);

            // extract data
            if (!$fileinfo->getIsdir()) {
                $fp = @fopen($output, "wb");
                if (!$fp) {
                    throw new ArchiveIOException('Could not open file for writing: '.$output);
                }

                $size = floor($header['size'] / 512);
                for ($i = 0; $i < $size; $i++) {
                    fwrite($fp, $this->readbytes(512), 512);
                }
                if (($header['size'] % 512) != 0) {
                    fwrite($fp, $this->readbytes(512), $header['size'] % 512);
                }

                fclose($fp);
                @touch($output, $fileinfo->getMtime());
                @chmod($output, $fileinfo->getMode());
            } else {
                $this->skipbytes(ceil($header['size'] / 512) * 512); // the size is usually 0 for directories
            }

            if(is_callable($this->callback)) {
                call_user_func($this->callback, $fileinfo);
            }
            $extracted[] = $fileinfo;
        }

        $this->close();
        return $extracted;
    }
public function extract($outdir, $strip = '', $exclude = '', $include = '')
    {
        if ($this->closed || !$this->file) {
            throw new ArchiveIOException('Can not read from a closed archive');
        }

        $outdir = rtrim($outdir, '/');
        @mkdir($outdir, 0777, true);
        if (!is_dir($outdir)) {
            throw new ArchiveIOException("Could not create directory '$outdir'");
        }

        $extracted = array();
        while ($dat = $this->readbytes(512)) {
            // read the file header
            $header = $this->parseHeader($dat);
            if (!is_array($header)) {
                continue;
            }
            $fileinfo = $this->header2fileinfo($header);

            // apply strip rules
            $fileinfo->strip($strip);

            // skip unwanted files
            if (!strlen($fileinfo->getPath()) || !$fileinfo->match($include, $exclude)) {
                $this->skipbytes(ceil($header['size'] / 512) * 512);
                continue;
            }

            // create output directory
            $output    = $outdir.'/'.$fileinfo->getPath();
            $directory = ($fileinfo->getIsdir()) ? $output : dirname($output);
            @mkdir($directory, 0777, true);

            // extract data
            if (!$fileinfo->getIsdir()) {
                $fp = @fopen($output, "wb");
                if (!$fp) {
                    throw new ArchiveIOException('Could not open file for writing: '.$output);
                }

                $size = floor($header['size'] / 512);
                for ($i = 0; $i < $size; $i++) {
                    fwrite($fp, $this->readbytes(512), 512);
                }
                if (($header['size'] % 512) != 0) {
                    fwrite($fp, $this->readbytes(512), $header['size'] % 512);
                }

                fclose($fp);
                @touch($output, $fileinfo->getMtime());
                @chmod($output, $fileinfo->getMode());
            } else {
                $this->skipbytes(ceil($header['size'] / 512) * 512); // the size is usually 0 for directories
            }

            if(is_callable($this->callback)) {
                call_user_func($this->callback, $fileinfo);
            }
            $extracted[] = $fileinfo;
        }

        $this->close();
        return $extracted;
    }
public function extract ( $ outdir , $ strip = '' , $ exclude = '' , $ include = '' ) { if ( $ this -> closed || ! $ this -> file ) { throw new ArchiveIOException ( 'Can not read from a closed archive' ) ; } $ outdir = rtrim ( $ outdir , '/' ) ; @ mkdir ( $ outdir , 0777 , true ) ; if ( ! is_dir ( $ outdir ) ) { throw new ArchiveIOException ( "Could not create directory '$outdir'" ) ; } $ extracted = array ( ) ; while ( $ dat = $ this -> readbytes ( 512 ) ) { // read the file header $ header = $ this -> parseHeader ( $ dat ) ; if ( ! is_array ( $ header ) ) { continue ; } $ fileinfo = $ this -> header2fileinfo ( $ header ) ; // apply strip rules $ fileinfo -> strip ( $ strip ) ; // skip unwanted files if ( ! strlen ( $ fileinfo -> getPath ( ) ) || ! $ fileinfo -> match ( $ include , $ exclude ) ) { $ this -> skipbytes ( ceil ( $ header [ 'size' ] / 512 ) * 512 ) ; continue ; } // create output directory $ output = $ outdir . '/' . $ fileinfo -> getPath ( ) ; $ directory = ( $ fileinfo -> getIsdir ( ) ) ? $ output : dirname ( $ output ) ; @ mkdir ( $ directory , 0777 , true ) ; // extract data if ( ! $ fileinfo -> getIsdir ( ) ) { $ fp = @ fopen ( $ output , "wb" ) ; if ( ! $ fp ) { throw new ArchiveIOException ( 'Could not open file for writing: ' . $ output ) ; } $ size = floor ( $ header [ 'size' ] / 512 ) ; for ( $ i = 0 ; $ i < $ size ; $ i ++ ) { fwrite ( $ fp , $ this -> readbytes ( 512 ) , 512 ) ; } if ( ( $ header [ 'size' ] % 512 ) != 0 ) { fwrite ( $ fp , $ this -> readbytes ( 512 ) , $ header [ 'size' ] % 512 ) ; } fclose ( $ fp ) ; @ touch ( $ output , $ fileinfo -> getMtime ( ) ) ; @ chmod ( $ output , $ fileinfo -> getMode ( ) ) ; } else { $ this -> skipbytes ( ceil ( $ header [ 'size' ] / 512 ) * 512 ) ; // the size is usually 0 for directories } if ( is_callable ( $ this -> callback ) ) { call_user_func ( $ this -> callback , $ fileinfo ) ; } $ extracted [ ] = $ fileinfo ; } $ this -> close ( ) ; return $ extracted ; }
Extract an existing TAR archive

The $strip parameter allows you to strip a certain number of path components from the filenames
found in the tar file, similar to the --strip-components feature of GNU tar. This is triggered when
an integer is passed as $strip.
Alternatively a fixed string prefix may be passed in $strip. If the filename matches this prefix,
the prefix will be stripped. It is recommended to give prefixes with a trailing slash.

By default this will extract all files found in the archive. You can restrict the output using the $include
and $exclude parameter. Both expect a full regular expression (including delimiters and modifiers). If
$include is set only files that match this expression will be extracted. Files that match the $exclude
expression will never be extracted. Both parameters can be used in combination. Expressions are matched against
stripped filenames as described above.

The archive is closed afer reading the contents, because rewinding is not possible in bzip2 streams.
Reopen the file with open() again if you want to do additional operations

@param string $outdir the target directory for extracting
@param int|string $strip either the number of path components or a fixed prefix to strip
@param string $exclude a regular expression of files to exclude
@param string $include a regular expression of files to include
@throws ArchiveIOException
@throws ArchiveCorruptedException
@return FileInfo[]
Extract an existing TAR archive