Actions

API15

JArchive/extract

From Joomla! Documentation

< API15:JArchive
Revision as of 17:20, 22 March 2010 by Doxiki (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Replacement filing cabinet.png
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.

[Edit Descripton]

Description:JArchive/extract

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;
}

[Edit See Also] SeeAlso:JArchive/extract

Examples

<CodeExamplesForm />