API16

API16:JArchiveBzip2/extract

From Joomla! Documentation

Revision as of 22:47, 22 March 2010 by Doxiki (talk | contribs) (New page: ===Description=== Extract a Bzip2 compressed file to a given path <span class="editsection" style="font-size:76%;"> <nowiki>[</nowiki>[[Description:JArchiveBzip2/extract|Edit Descript...)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Description

Extract a Bzip2 compressed file to a given path

[Edit Descripton]

Template:Description:JArchiveBzip2/extract

Syntax

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

Returns

boolean True if successful

Defined in

libraries/joomla/filesystem/archive/bzip2.php

Importing

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

Source Body

function extract($archive, $destination, $options = array ())
{
        // Initialise variables.
        $this->_data = null;

        if (!extension_loaded('bz2')) {
                $this->set('error.message', 'BZip2 Not Supported');
                return JError::raiseWarning(100, $this->get('error.message'));
        }

        /* // old style: read the whole file and then parse it
        if (!$this->_data = JFile::read($archive)) {
                $this->set('error.message', 'Unable to read archive');
                return JError::raiseWarning(100, $this->get('error.message'));
        }

        $buffer = bzdecompress($this->_data);
        unset($this->_data);
        if (empty ($buffer)) {
                $this->set('error.message', 'Unable to decompress data');
                return JError::raiseWarning(100, $this->get('error.message'));
        }

        if (JFile::write($destination, $buffer) === false) {
                $this->set('error.message', 'Unable to write archive');
                return JError::raiseWarning(100, $this->get('error.message'));
        }
        //*/

        // New style! streams!
        $input =& JFactory::getStream();
        $input->set('processingmethod','bz'); // use bzip
        if(!$input->open($archive)) {
                $this->set('error.message', 'Unable to read archive (bz2)');
                return JError::raiseWarning(100, $this->get('error.message'));
        }

        $output =& JFactory::getStream();
        if(!$output->open($destination, 'w')) {
                $this->set('error.message', 'Unable to write archive (bz2)');
                $input->close(); // close the previous file
                return JError::raiseWarning(100, $this->get('error.message'));
        }

        $written = 0;
        do {
                $this->_data = $input->read($input->get('chunksize', 8196));
                if($this->_data) {
                        if(!$output->write($this->_data)) {
                                $this->set('error.message', 'Unable to write file (bz2)');
                                return JError::raiseWarning(100, $this->get('error.message'));
                        }
                }
        } while ($this->_data);
        $output->close();
        $input->close();
        return true;
}

[Edit See Also] Template:SeeAlso:JArchiveBzip2/extract

Examples

<CodeExamplesForm />