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