API16

Difference between revisions of "JArchiveTar/extract"

From Joomla! Documentation

< API16:JArchiveTar
(New page: ===Description=== Extract a ZIP compressed file to a given path <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>Edit Descripton...)
 
m (removing red link to edit, no existant pages)
Line 3: Line 3:
  
 
<span class="editsection" style="font-size:76%;">
 
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[Description:JArchiveTar/extract|Edit Descripton]]<nowiki>]</nowiki>
+
<nowiki>[<! removed edit link to red link >]</nowiki>
 
</span>
 
</span>
  
{{Description:JArchiveTar/extract}}
+
<! removed transcluded page call, red link never existed >
  
 
===Syntax===
 
===Syntax===
Line 127: Line 127:
  
 
<span class="editsection" style="font-size:76%;">
 
<span class="editsection" style="font-size:76%;">
<nowiki>[</nowiki>[[SeeAlso:JArchiveTar/extract|Edit See Also]]<nowiki>]</nowiki>
+
<nowiki>[<! removed edit link to red link >]</nowiki>
 
</span>
 
</span>
{{SeeAlso:JArchiveTar/extract}}
+
<! removed transcluded page call, red link never existed >
  
 
===Examples===
 
===Examples===
Line 142: Line 142:
 
  format= ,,,
 
  format= ,,,
 
</dpl>
 
</dpl>
 +
[[Category:Archived pages API16]]

Revision as of 21:49, 12 May 2013

The "API16" 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.

Description[edit]

Extract a ZIP compressed file to a given path

[<! removed edit link to red link >]

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

Syntax[edit]

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[edit]

boolean True if successful

Defined in[edit]

libraries/joomla/filesystem/archive/tar.php

Importing[edit]

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

Source Body[edit]

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[edit]

<CodeExamplesForm />