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