Version: current

     1 <?php
     2 /**
     3  * Implementation of .tar, .tar.gz, and .tar.bz2 archive handler.
     4  * @package sapphire
     5  * @subpackage filesystem
     6  */
     7 class TarballArchive extends Archive {
     8     private $filename = '';
     9     private $compressionModifiers = '';
    10
    11
    12     function __construct($filename) {
    13         $this->filename = $filename;
    14
    15         if(substr($filename, strlen($filename) - strlen('.gz')) == '.gz' ||
    16             substr($filename, strlen($filename) - strlen('.tgz')) == '.tgz') {
    17             $this->compressionModifiers = 'z';
    18         } else if(substr($filename, strlen($filename) - strlen('.bz2')) == '.bz2') {
    19             $compressionModifiers = 'j';
    20         }
    21     }
    22
    23     function listing() {
    24         // Call tar on the command line to get the info we need
    25         $base = BASE_PATH;
    26         $command = "tar -tv{$this->compressionModifiers}f $base/$this->filename";
    27         $consoleList = `$command`;
    28
    29         $listing = array();
    30         // Seperate into an array of lines
    31         $listItems = explode("\n", $consoleList);
    32
    33         foreach($listItems as $listItem) {
    34             // The path is the last thing on the line
    35             $fullpath = substr($listItem, strrpos($listItem, ' ') + 1);
    36             $path = explode('/', $fullpath);
    37             $item = array();
    38
    39             // The first part of the line is the permissions - the first character will be d if it is a directory
    40             $item['type'] = (substr($listItem, 0, 1) == 'd') ? 'directory' : 'file';
    41             if($item['type'] == 'directory') {
    42                 $item['listing'] = array();
    43                 // If it's a directory, the path will have a slash on the end, so get rid of it.
    44                 array_pop($path);
    45             }
    46
    47             // The name of the file/directory is the last item on the path
    48             $name = array_pop($path);
    49             if($name == '') {
    50                 continue;
    51             }
    52
    53             $item['path'] = implode('/', $path);
    54
    55             // Put the item in the right place
    56             $dest = &$listing;
    57             foreach($path as $folder) {
    58                 // If the directory doesn't exist, create it
    59                 if(!isset($dest[$folder])) {
    60                     $dest[$folder] = array();
    61                     $dest[$folder]['listing'] = array();
    62                     $dest[$folder]['type'] = 'directory';
    63                 }
    64                 $dest = &$dest[$folder]['listing'];
    65             }
    66
    67             // If this is a directory and it's listing has already been created, copy the the listing
    68             if($item['type'] == 'directory' && isset($dest[$name]['listing'])) {
    69                 $item['listing'] = $dest[$name]['listing'];
    70             }
    71             $dest[$name] = $item;
    72         }
    73
    74
    75         return $listing;
    76     }
    77
    78     function extractTo($destination, $entires = null) {
    79         if(!isset($entries)) {
    80             $command = "tar -xv{$this->compressionModifiers}f ".Director::baseFolder()."/$this->filename --directory $destination";
    81             $output = `$command`;
    82         }
    83     }
    84
    85 }
    86
    87 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -