PicoraTar

Create and unpack TAR files.

/** This class is a thin wrapper around Josh Barger's PHP TAR implementation.


Method Overview

Return Visibility Name Parameters
bool static public pack (string $target_file , array $file_list , bool $gzip )
bool static public unpack (string $file , bool $location )

Method Detail

static public pack()

Parameter Type Name Description
string $target_file
array $file_list
bool $gzip

/**

static public unpack()

Parameter Type Name Description
string $file
bool $location

/**

Declared in: PicoraTar.php

class PicoraTar {    
    /**
     * @param string $target_file
     * @param array $file_list
     * @param bool $gzip
     * @return bool
     */
    static public function pack($target_file,$file_list,$gzip = false){
        $t = new Tar;
        foreach($file_list as $item)
            $t->addFile(self::normalizePath($item));
        $response = $t->toTar(self::normalizePath($target_file),$gzip);
        return $response;
    }
    /**
     * @param string $file
     * @param bool $location
     * @return bool
     */
    static public function unpack($file,$loc = false){
        $t = new Tar;
        if(!$t->openTar(self::normalizePath($file)))
            return false;
        if(!$loc)
            return $t->files;
        if(!file_exists(self::normalizePath($loc)))
            mkdir($loc,0777,true);
        if(substr($loc,0,-1) != '/')
            $loc .= '/';
        foreach($t->files as $file){
            if(!file_exists(self::normalizePath($loc.dirname($file['name']))))
                mkdir(self::normalizePath($loc.dirname($file['name'])),0777,true);
            file_put_contents(self::normalizePath($loc.$file['name']),$file['file']);
            chmod(self::normalizePath($loc.$file['name']),0777);
        }
        return true;
    }
    static protected function normalizePath($path){
        $path = preg_replace('/\/[^\/]+\/\.\./','',$path);
        return (PHP_OS == 'WIN32' || PHP_OS == 'WINNT')
            ? str_replace(chr(47),chr(92),$path)
            : str_replace(chr(92),chr(47),$path)
        ;
    }
}