Overview

Packages

  • Compress

Classes

  • Horde_Compress
  • Horde_Compress_Base
  • Horde_Compress_Dbx
  • Horde_Compress_Exception
  • Horde_Compress_Gzip
  • Horde_Compress_Rar
  • Horde_Compress_Tar
  • Horde_Compress_Tnef
  • Horde_Compress_Translation
  • Horde_Compress_Zip
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * This class allows tar files to be read.
  4:  *
  5:  * Copyright 2002-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @author   Michael Cochrane <mike@graftonhall.co.nz>
 11:  * @author   Michael Slusarz <slusarz@horde.org>
 12:  * @category Horde
 13:  * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
 14:  * @package  Compress
 15:  */
 16: class Horde_Compress_Tar extends Horde_Compress_Base
 17: {
 18:     /**
 19:      */
 20:     public $canDecompress = true;
 21: 
 22:     /**
 23:      * Tar file types.
 24:      *
 25:      * @var array
 26:      */
 27:     protected $_types = array(
 28:         0x0   =>  'Unix file',
 29:         0x30  =>  'File',
 30:         0x31  =>  'Link',
 31:         0x32  =>  'Symbolic link',
 32:         0x33  =>  'Character special file',
 33:         0x34  =>  'Block special file',
 34:         0x35  =>  'Directory',
 35:         0x36  =>  'FIFO special file',
 36:         0x37  =>  'Contiguous file'
 37:     );
 38: 
 39:     /**
 40:      * Tar file flags.
 41:      *
 42:      * @var array
 43:      */
 44:     protected $_flags = array(
 45:         'FTEXT'     =>  0x01,
 46:         'FHCRC'     =>  0x02,
 47:         'FEXTRA'    =>  0x04,
 48:         'FNAME'     =>  0x08,
 49:         'FCOMMENT'  =>  0x10
 50:     );
 51: 
 52:     /**
 53:      * @return array  Tar file data:
 54:      * <pre>
 55:      * KEY: Position in the array
 56:      * VALUES:
 57:      *   attr - File attributes
 58:      *   data - Raw file contents
 59:      *   date - File modification time
 60:      *   name - Filename
 61:      *   size - Original file size
 62:      *   type - File type
 63:      * </pre>
 64:      *
 65:      * @throws Horde_Compress_Exception
 66:      */
 67:     public function decompress($data, array $params = array())
 68:     {
 69:         $data_len = strlen($data);
 70:         $position = 0;
 71:         $return_array = array();
 72: 
 73:         while ($position < $data_len) {
 74:             $info = @unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/Ctypeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", substr($data, $position));
 75:             if (!$info) {
 76:                 throw new Horde_Compress_Exception(Horde_Compress_Translation::t("Unable to decompress data."));
 77:             }
 78: 
 79:             $position += 512;
 80:             $contents = substr($data, $position, octdec($info['size']));
 81:             $position += ceil(octdec($info['size']) / 512) * 512;
 82: 
 83:             if ($info['filename']) {
 84:                 $file = array(
 85:                     'attr' => null,
 86:                     'data' => null,
 87:                     'date' => octdec($info['mtime']),
 88:                     'name' => trim($info['filename']),
 89:                     'size' => octdec($info['size']),
 90:                     'type' => isset($this->_types[$info['typeflag']]) ? $this->_types[$info['typeflag']] : null
 91:                 );
 92: 
 93:                 if (($info['typeflag'] == 0) ||
 94:                     ($info['typeflag'] == 0x30) ||
 95:                     ($info['typeflag'] == 0x35)) {
 96:                     /* File or folder. */
 97:                     $file['data'] = $contents;
 98: 
 99:                     $mode = hexdec(substr($info['mode'], 4, 3));
100:                     $file['attr'] =
101:                         (($info['typeflag'] == 0x35) ? 'd' : '-') .
102:                         (($mode & 0x400) ? 'r' : '-') .
103:                         (($mode & 0x200) ? 'w' : '-') .
104:                         (($mode & 0x100) ? 'x' : '-') .
105:                         (($mode & 0x040) ? 'r' : '-') .
106:                         (($mode & 0x020) ? 'w' : '-') .
107:                         (($mode & 0x010) ? 'x' : '-') .
108:                         (($mode & 0x004) ? 'r' : '-') .
109:                         (($mode & 0x002) ? 'w' : '-') .
110:                         (($mode & 0x001) ? 'x' : '-');
111:                 }
112: 
113:                 $return_array[] = $file;
114:             }
115:         }
116: 
117:         return $return_array;
118:     }
119: 
120: }
121: 
API documentation generated by ApiGen