Actions

API16

JArchiveTar/extract

From Joomla! Documentation

< API16:JArchiveTar
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.

Contents

Description

Extract a ZIP compressed file to a given path

[<! removed edit link to red link >]

<! removed transcluded page call, red link never existed >

Syntax

extract($archive, $destination, $options=array())
Parameter Name Default Value Description
$archive $archive Path to ZIP archive to extract
$destination $destination Path to extract archive into
$options array() $options Extraction options [unused]

Returns

boolean True if successful

Defined in

libraries/joomla/filesystem/archive/tar.php

Importing

jimport( 'joomla.filesystem.archive.tar' );

Source Body

function extract($archive, $destination, $options = array ())
{
        // Initialise variables.
        $this->_data = null;
        $this->_metadata = null;
 
        $stream =& JFactory::getStream();
        if(!$stream->open($archive, 'rb'))
        {
                $this->set('error.message', 'Unable to read archive');
                return JError::raiseWarning(100, $this->get('error.message'));
        }
 
        $position = 0;
        $return_array = array ();
        $i = 0;
        $chunksize = 512; // tar has items in 512 byte packets
 
        while($entry = $stream->read($chunksize)) {
                //$entry =& $this->_data[$i];
                $info = @ unpack("a100filename/a8mode/a8uid/a8gid/a12size/a12mtime/a8checksum/Ctypeflag/a100link/a6magic/a2version/a32uname/a32gname/a8devmajor/a8devminor", $entry);
                if (!$info) {
                        $this->set('error.message', 'Unable to decompress data');
                        return JError::raiseWarning(100, $this->get('error.message'));
                }
 
                $size = octdec($info['size']);
                $bsize = ceil($size / $chunksize) * $chunksize;
                $contents = '';
                if($size) {
                        //$contents = fread($this->_fh, $size);
                        $contents = substr($stream->read($bsize),0, octdec($info['size']));
                }
 
                if ($info['filename']) {
                        $file = array (
                                'attr' => null,
                                'data' => null,
                                'date' => octdec($info['mtime']),
                                'name' => trim($info['filename']),
                                'size' => octdec($info['size']),
                                'type' => isset ($this->_types[$info['typeflag']]) ? $this->_types[$info['typeflag']] : null);
 
                        if (($info['typeflag'] == 0) || ($info['typeflag'] == 0x30) || ($info['typeflag'] == 0x35)) {
                                /* File or folder. */
                                $file['data'] = $contents;
 
                                $mode = hexdec(substr($info['mode'], 4, 3));
                                $file['attr'] = (($info['typeflag'] == 0x35) ? 'd' : '-') .
                                (($mode & 0x400) ? 'r' : '-') .
                                (($mode & 0x200) ? 'w' : '-') .
                                (($mode & 0x100) ? 'x' : '-') .
                                (($mode & 0x040) ? 'r' : '-') .
                                (($mode & 0x020) ? 'w' : '-') .
                                (($mode & 0x010) ? 'x' : '-') .
                                (($mode & 0x004) ? 'r' : '-') .
                                (($mode & 0x002) ? 'w' : '-') .
                                (($mode & 0x001) ? 'x' : '-');
                        } else {
                                /* Some other type. */
                        }
 
                        $type = strtolower( $file['type'] );
                        if ($type == 'file' || $type == 'unix file')
                        {
                                $path = JPath::clean($destination.DS.$file['name']);
                                // Make sure the destination folder exists
                                if (!JFolder::create(dirname($path)))
                                {
                                        $this->set('error.message', 'Unable to create destination');
                                        return JError::raiseWarning(100, $this->get('error.message'));
                }
                                if (JFile::write($path, $contents, true) === false)
                                {
                                        $this->set('error.message', 'Unable to write entry');
                                        return JError::raiseWarning(100, $this->get('error.message'));
        }
                                $contents = ''; // reclaim some memory
                        }
                }
        }
        $stream->close();
        return true;
}

[<! removed edit link to red link >] <! removed transcluded page call, red link never existed >

Examples

<CodeExamplesForm />