Support Joomla!

Packages

Package: Joomla-Framework

License

Content on this site is copyright © 2005 - 2008 Open Source Matters Inc and can be used in accordance with the Joomla! Electronic Documentation License. Some parts of this website may be subject to other licenses.
Source code for file /joomla/filesystem/archive.php

Documentation is available at archive.php

  1. <?php
  2. /**
  3.  * @version        $Id: archive.php 10381 2008-06-01 03:35:53Z pasamio $
  4.  * @package        Joomla.Framework
  5.  * @subpackage    FileSystem
  6.  * @copyright    Copyright (C) 2005 - 2008 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. /**
  16.  * An Archive handling class
  17.  *
  18.  * @static
  19.  * @package     Joomla.Framework
  20.  * @subpackage    FileSystem
  21.  * @since        1.5
  22.  */
  23. class JArchive
  24. {
  25.     /**
  26.      * @param    string    The name of the archive file
  27.      * @param    string    Directory to unpack into
  28.      * @return    boolean    True for success
  29.      */
  30.     function extract$archivename$extractdir)
  31.     {
  32.         jimport('joomla.filesystem.file');
  33.         jimport('joomla.filesystem.folder');
  34.         $untar false;
  35.         $result false;
  36.         $ext JFile::getExt(strtolower($archivename));
  37.         // check if a tar is embedded...gzip/bzip2 can just be plain files!
  38.         if (JFile::getExt(JFile::stripExt(strtolower($archivename))) == 'tar'{
  39.             $untar true;
  40.         }
  41.  
  42.         switch ($ext)
  43.         {
  44.             case 'zip':
  45.                 $adapter =JArchive::getAdapter('zip');
  46.                 if ($adapter{
  47.                     $result $adapter->extract($archivename$extractdir);
  48.                 }
  49.                 break;
  50.             case 'tar':
  51.                 $adapter =JArchive::getAdapter('tar');
  52.                 if ($adapter{
  53.                     $result $adapter->extract($archivename$extractdir);
  54.                 }
  55.                 break;
  56.             case 'tgz'  :
  57.                 $untar true;    // This format is a tarball gzip'd
  58.             case 'gz'   :    // This may just be an individual file (e.g. sql script)
  59.             case 'gzip' :
  60.                 $adapter =JArchive::getAdapter('gzip');
  61.                 if ($adapter)
  62.                 {
  63.                     $config =JFactory::getConfig();
  64.                     $tmpfname $config->getValue('config.tmp_path').DS.uniqid('gzip');
  65.                     $gzresult $adapter->extract($archivename$tmpfname);
  66.                     if (JError::isError($gzresult))
  67.                     {
  68.                         @unlink($tmpfname);
  69.                         return false;
  70.                     }
  71.                     if($untar)
  72.                     {
  73.                         // Try to untar the file
  74.                         $tadapter =JArchive::getAdapter('tar');
  75.                         if ($tadapter{
  76.                             $result $tadapter->extract($tmpfname$extractdir);
  77.                         }
  78.                     }
  79.                     else
  80.                     {
  81.                         $path JPath::clean($extractdir);
  82.                         JFolder::create($path);
  83.                         $result JFile::copy($tmpfname,$path.DS.JFile::stripExt(JFile::getName(strtolower($archivename))));
  84.                     }
  85.                     @unlink($tmpfname);
  86.                 }
  87.                 break;
  88.             case 'tbz2' :
  89.                 $untar true// This format is a tarball bzip2'd
  90.             case 'bz2'  :    // This may just be an individual file (e.g. sql script)
  91.             case 'bzip2':
  92.                 $adapter =JArchive::getAdapter('bzip2');
  93.                 if ($adapter)
  94.                 {
  95.                     $config =JFactory::getConfig();
  96.                     $tmpfname $config->getValue('config.tmp_path').DS.uniqid('bzip2');
  97.                     $bzresult $adapter->extract($archivename$tmpfname);
  98.                     if (JError::isError($bzresult))
  99.                     {
  100.                         @unlink($tmpfname);
  101.                         return false;
  102.                     }
  103.                     if ($untar)
  104.                     {
  105.                         // Try to untar the file
  106.                         $tadapter =JArchive::getAdapter('tar');
  107.                         if ($tadapter{
  108.                             $result $tadapter->extract($tmpfname$extractdir);
  109.                         }
  110.                     }
  111.                     else
  112.                     {
  113.                         $path JPath::clean($extractdir);
  114.                         JFolder::create($path);
  115.                         $result JFile::copy($tmpfname,$path.DS.JFile::stripExt(JFile::getName(strtolower($archivename))));
  116.                     }
  117.                     @unlink($tmpfname);
  118.                 }
  119.                 break;
  120.             default:
  121.                 JError::raiseWarning(10JText::_('UNKNOWNARCHIVETYPE'));
  122.                 return false;
  123.                 break;
  124.         }
  125.  
  126.         if ($result || JError::isError($result)) {
  127.             return false;
  128.         }
  129.         return true;
  130.     }
  131.  
  132.     function &getAdapter($type)
  133.     {
  134.         static $adapters;
  135.  
  136.         if (!isset($adapters)) {
  137.             $adapters array();
  138.         }
  139.  
  140.         if (!isset($adapters[$type]))
  141.         {
  142.             // Try to load the adapter object
  143.             $class 'JArchive'.ucfirst($type);
  144.  
  145.             if (!class_exists($class))
  146.             {
  147.                 $path dirname(__FILE__).DS.'archive'.DS.strtolower($type).'.php';
  148.                 if (file_exists($path)) {
  149.                     require_once($path);
  150.                 else {
  151.                     JError::raiseError(500,JText::_('Unable to load archive'));
  152.                 }
  153.             }
  154.  
  155.             $adapters[$typenew $class();
  156.         }
  157.         return $adapters[$type];
  158.     }
  159.  
  160.     /**
  161.      * @param    string    The name of the archive
  162.      * @param    mixed    The name of a single file or an array of files
  163.      * @param    string    The compression for the archive
  164.      * @param    string    Path to add within the archive
  165.      * @param    string    Path to remove within the archive
  166.      * @param    boolean    Automatically append the extension for the archive
  167.      * @param    boolean    Remove for source files
  168.      */
  169.     function create($archive$files$compress 'tar'$addPath ''$removePath ''$autoExt false$cleanUp false)
  170.     {
  171.         jimport'pear.archive_tar.Archive_Tar' );
  172.  
  173.         if (is_string($files)) {
  174.             $files array ($files);
  175.         }
  176.         if ($autoExt{
  177.             $archive .= '.'.$compress;
  178.         }
  179.  
  180.         $tar new Archive_Tar$archive$compress );
  181.         $tar->setErrorHandling(PEAR_ERROR_PRINT);
  182.         $tar->createModify$files$addPath$removePath );
  183.  
  184.         if ($cleanUp{
  185.             JFile::delete$files );
  186.         }
  187.         return $tar;
  188.     }
  189. }

Documentation generated on Mon, 22 Sep 2008 12:07:29 +0100 by phpDocumentor 1.3.1