JArchive/extract
From Joomla! Documentation
< API15:JArchive
The "API15" namespace is an archived namespace. This page contains information for a Joomla! version which is no longer supported. It exists only as a historical reference, it will not be improved and its content may be incomplete and/or contain broken links.
<! removed transcluded page call, red link never existed >
Syntax[edit]
extract($archivename, $extractdir)
Parameter Name | Default Value | Description |
---|---|---|
$archivename | The name of the archive file | |
$extractdir | Directory to unpack into |
Returns[edit]
boolean True for success
Defined in[edit]
libraries/joomla/filesystem/archive.php
Importing[edit]
jimport( 'joomla.filesystem.archive' );
Source Body[edit]
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;
}
<! removed transcluded page call, red link never existed >
Examples[edit]
Code Examples[edit]