Support Joomla!

Joomla! 1.5 Documentation

Packages

Package: Joomla-Framework

Developer Network License

The Joomla! Developer Network content is © copyright 2006 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution- NonCommercial- ShareAlike 2.5
Source code for file /joomla/filesystem/archive/tar.php

Documentation is available at tar.php

  1. <?php
  2. /**
  3.  * @version        $Id: archive.php 6138 2007-01-02 03:44:18Z eddiea $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    FileSystem
  6.  * @copyright    Copyright (C) 2005 - 2007 Open Source Matters. All rights reserved.
  7.  * @license        GNU/GPL, see LICENSE.php
  8.  *  Joomla! is free software. This version may have been modified pursuant
  9.  *  to the GNU General Public License, and as distributed it includes or
  10.  *  is derivative of works licensed under the GNU General Public License or
  11.  *  other free or open source software licenses.
  12.  *  See COPYRIGHT.php for copyright notices and details.
  13.  */
  14.  
  15. // Check to ensure this file is within the rest of the framework
  16. defined('JPATH_BASE'or die();
  17.  
  18. /**
  19.  * Tar format adapter for the JArchive class
  20.  *
  21.  * This class is inspired from and draws heavily in code and concept from the Compress package of
  22.  * The Horde Project <http://www.horde.org>
  23.  *
  24.  * @contributor  Michael Slusarz <[email protected]>
  25.  * @contributor  Michael Cochrane <[email protected]>
  26.  *
  27.  * @author        Louis Landry <[email protected]>
  28.  * @package     Joomla.Framework
  29.  * @subpackage    FileSystem
  30.  * @since        1.5
  31.  */
  32. class JArchiveTar extends JObject
  33. {
  34.     /**
  35.      * Tar file types.
  36.      * @var array 
  37.      */
  38.     var $_types = array (
  39.         0x0 => 'Unix file',
  40.         0x30 => 'File',
  41.         0x31 => 'Link',
  42.         0x32 => 'Symbolic link',
  43.         0x33 => 'Character special file',
  44.         0x34 => 'Block special file',
  45.         0x35 => 'Directory',
  46.         0x36 => 'FIFO special file',
  47.         0x37 => 'Contiguous file'
  48.     );
  49.  
  50.     /**
  51.      * Tar file flags.
  52.      * @var array 
  53.      */
  54.     var $_flags = array (
  55.         'FTEXT' => 0x01,
  56.         'FHCRC' => 0x02,
  57.         'FEXTRA' => 0x04,
  58.         'FNAME' => 0x08,
  59.         'FCOMMENT' => 0x10
  60.     );
  61.  
  62.     /**
  63.      * Tar file data buffer
  64.      * @var string 
  65.      */
  66.     var $_data = null;
  67.  
  68.     /**
  69.      * Tar file metadata array
  70.      * @var array 
  71.      */
  72.     var $_metadata = null;
  73.  
  74.     /**
  75.     * Extract a ZIP compressed file to a given path
  76.     *
  77.     * @access    public
  78.     * @param    string    $archive        Path to ZIP archive to extract
  79.     * @param    string    $destination    Path to extract archive into
  80.     * @param    array    $options        Extraction options [unused]
  81.     * @return    boolean    True if successful
  82.     * @since    1.5
  83.     */
  84.     function extract($archive$destination$options array ())
  85.     {
  86.         // Initialize variables
  87.         $this->_data = null;
  88.         $this->_metadata = null;
  89.  
  90.         if (!$this->_data = JFile::read($archive)) {
  91.             $this->set('error.message''Unable to read archive');
  92.             return JError::raiseWarning(100$this->get('error.message'));
  93.         }
  94.  
  95.         if (!$this->_getTarInfo($this->_data)) {
  96.             return JError::raiseWarning(100$this->get('error.message'));
  97.         }
  98.  
  99.         for ($i=0,$n=count($this->_metadata);$i<$n;$i++{
  100.             if ($this->_metadata[$i]['type'== 'File'{
  101.                 $buffer $this->_metadata[$i]['data'];
  102.                 $path JPath::clean($destination.DS.$this->_metadata[$i]['name']);
  103.                 // Make sure the destination folder exists
  104.                 if (!JFolder::create(dirname($path))) {
  105.                     $this->set('error.message''Unable to create destination');
  106.                     return JError::raiseWarning(100$this->get('error.message'));
  107.                 }
  108.                 if (JFile::write($path$buffer=== false{
  109.                     $this->set('error.message''Unable to write entry');
  110.                     return JError::raiseWarning(100$this->get('error.message'));
  111.                 }
  112.             }
  113.         }
  114.         return true;
  115.     }
  116.  
  117.     /**
  118.      * Get the list of files/data from a Tar archive buffer.
  119.      *
  120.      * @access    private
  121.      * @param     string    $data    The Tar archive buffer.
  122.      * @return    array    Archive metadata array
  123.      *  <pre>
  124.      *  KEY: Position in the array
  125.      *  VALUES: 'attr'  --  File attributes
  126.      *          'data'  --  Raw file contents
  127.      *          'date'  --  File modification time
  128.      *          'name'  --  Filename
  129.      *          'size'  --  Original file size
  130.      *          'type'  --  File type
  131.      *  </pre>
  132.      * @since    1.5
  133.      */
  134.     function _getTarInfo($data)
  135.     {
  136.         $position 0;
  137.         $return_array array ();
  138.  
  139.         while ($position strlen($data))
  140.         {
  141.             $info unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/Ctypeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor"substr($data$position));
  142.             if (!$info{
  143.                 $this->set('error.message''Unable to decompress data');
  144.                 return false;
  145.             }
  146.  
  147.             $position += 512;
  148.             $contents substr($data$positionoctdec($info['size']));
  149.             $position += ceil(octdec($info['size']512512;
  150.  
  151.             if ($info['filename']{
  152.                 $file array (
  153.                     'attr' => null,
  154.                     'data' => null,
  155.                     'date' => octdec($info['mtime']
  156.                 )'name' => trim($info['filename'])'size' => octdec($info['size'])'type' => isset ($this->_types[$info['typeflag']]$this->_types[$info['typeflag']] null);
  157.  
  158.                 if (($info['typeflag'== 0|| ($info['typeflag'== 0x30|| ($info['typeflag'== 0x35)) {
  159.                     /* File or folder. */
  160.                     $file['data'$contents;
  161.  
  162.                     $mode hexdec(substr($info['mode']43));
  163.                     $file['attr'(($info['typeflag'== 0x35'd' '-'.
  164.                      (($mode 0x400'r' '-'.
  165.                      (($mode 0x200'w' '-'.
  166.                      (($mode 0x100'x' '-'.
  167.                      (($mode 0x040'r' '-'.
  168.                      (($mode 0x020'w' '-'.
  169.                      (($mode 0x010'x' '-'.
  170.                      (($mode 0x004'r' '-'.
  171.                      (($mode 0x002'w' '-'.
  172.                      (($mode 0x001'x' '-');
  173.                 else {
  174.                     /* Some other type. */
  175.                 }
  176.                 $return_array[$file;
  177.             }
  178.         }
  179.         $this->_metadata = $return_array;
  180.         return true;
  181.     }
  182. }
  183. ?>

Documentation generated on Mon, 05 Mar 2007 21:27:50 +0000 by phpDocumentor 1.3.1