english/libraries/joomla/filesystem/archive.php
/**
	 * Extract an archive file to a directory.
	 *
	 * @param   string  $archivename  The name of the archive file
	 * @param   string  $extractdir   Directory to unpack into
	 *
	 * @return  boolean  True for success
	 * @since   11.1
	 */
Extract an archive file to a directory.
@param   string  $archivename  The name of the archive file
@param   string  $extractdir   Directory to unpack into

@return  boolean  True for success
@since   11.1
Extract an archive file to a directory . @param string $archivename The name of the archive file @param string $extractdir Directory to unpack into @return boolean True for success @since 11 . 1
public static function extract($archivename, $extractdir)
	{
		jimport('joomla.filesystem.file');
		jimport('joomla.filesystem.folder');
		$untar = false;
		$result = false;
		$ext = JFile::getExt(strtolower($archivename));
		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;
			case 'gz':
			case 'gzip':
				$adapter = JArchive::getAdapter('gzip');
				if ($adapter) {
					$config		= JFactory::getConfig();
					$tmpfname	= $config->get('tmp_path') . '/' . uniqid('gzip');
					$gzresult	= $adapter->extract($archivename, $tmpfname);
					if (JError::isError($gzresult)) {
						@unlink($tmpfname);
						return false;
					}
					if ($untar) {
						$tadapter = JArchive::getAdapter('tar');
						if ($tadapter) {
							$result = $tadapter->extract($tmpfname, $extractdir);
						}
					}
					else {
						$path = JPath::clean($extractdir);
						JFolder::create($path);
						$result = JFile::copy(
							$tmpfname,
							$path . '/' . JFile::stripExt(JFile::getName(strtolower($archivename))), null, 1
						);
					}
					@unlink($tmpfname);
				}
				break;
			case 'tbz2' :
				$untar = true;
			case 'bz2'  :
			case 'bzip2':
				$adapter = JArchive::getAdapter('bzip2');
				if ($adapter) {
					$config		= JFactory::getConfig();
					$tmpfname	= $config->get('tmp_path') . '/' . uniqid('bzip2');
					$bzresult	= $adapter->extract($archivename, $tmpfname);
					if (JError::isError($bzresult)) {
						@unlink($tmpfname);
						return false;
					}
					if ($untar) {
						$tadapter = JArchive::getAdapter('tar');
						if ($tadapter) {
							$result = $tadapter->extract($tmpfname, $extractdir);
						}
					}
					else {
						$path = JPath::clean($extractdir);
						JFolder::create($path);
						$result = JFile::copy(
							$tmpfname,
							$path . '/' . JFile::stripExt(JFile::getName(strtolower($archivename))), null, 1
						);
					}
					@unlink($tmpfname);
				}
				break;
			default:
				JError::raiseWarning(10, JText::_('JLIB_FILESYSTEM_UNKNOWNARCHIVETYPE'));
				return false;
				break;
		}
		if (! $result || JError::isError($result)) {
			return false;
		}
		return true;
	}
public static function extract ( $ archivename , $ extractdir ) { jimport ( 'joomla.filesystem.file' ) ; jimport ( 'joomla.filesystem.folder' ) ; $ untar = false ; $ result = false ; $ ext = JFile :: getExt ( strtolower ( $ archivename ) ) ; 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 ; case 'gz' : case 'gzip' : $ adapter = JArchive :: getAdapter ( 'gzip' ) ; if ( $ adapter ) { $ config = JFactory :: getConfig ( ) ; $ tmpfname = $ config -> get ( 'tmp_path' ) . '/' . uniqid ( 'gzip' ) ; $ gzresult = $ adapter -> extract ( $ archivename , $ tmpfname ) ; if ( JError :: isError ( $ gzresult ) ) { @ unlink ( $ tmpfname ) ; return false ; } if ( $ untar ) { $ tadapter = JArchive :: getAdapter ( 'tar' ) ; if ( $ tadapter ) { $ result = $ tadapter -> extract ( $ tmpfname , $ extractdir ) ; } } else { $ path = JPath :: clean ( $ extractdir ) ; JFolder :: create ( $ path ) ; $ result = JFile :: copy ( $ tmpfname , $ path . '/' . JFile :: stripExt ( JFile :: getName ( strtolower ( $ archivename ) ) ) , null , 1 ) ; } @ unlink ( $ tmpfname ) ; } break ; case 'tbz2' : $ untar = true ; case 'bz2' : case 'bzip2' : $ adapter = JArchive :: getAdapter ( 'bzip2' ) ; if ( $ adapter ) { $ config = JFactory :: getConfig ( ) ; $ tmpfname = $ config -> get ( 'tmp_path' ) . '/' . uniqid ( 'bzip2' ) ; $ bzresult = $ adapter -> extract ( $ archivename , $ tmpfname ) ; if ( JError :: isError ( $ bzresult ) ) { @ unlink ( $ tmpfname ) ; return false ; } if ( $ untar ) { $ tadapter = JArchive :: getAdapter ( 'tar' ) ; if ( $ tadapter ) { $ result = $ tadapter -> extract ( $ tmpfname , $ extractdir ) ; } } else { $ path = JPath :: clean ( $ extractdir ) ; JFolder :: create ( $ path ) ; $ result = JFile :: copy ( $ tmpfname , $ path . '/' . JFile :: stripExt ( JFile :: getName ( strtolower ( $ archivename ) ) ) , null , 1 ) ; } @ unlink ( $ tmpfname ) ; } break ; default : JError :: raiseWarning ( 10 , JText :: _ ( 'JLIB_FILESYSTEM_UNKNOWNARCHIVETYPE' ) ) ; return false ; break ; } if ( ! $ result || JError :: isError ( $ result ) ) { return false ; } return true ; }
Extract an archive file to a directory.
Extract an archive file to a directory .
// Check if a tar is embedded...gzip/bzip2 can just be plain files! // This format is a tarball gzip'd // This may just be an individual file (e.g. sql script) // Try to untar the file // This format is a tarball bzip2'd // This may just be an individual file (e.g. sql script) // Try to untar the file