Example #1
0
 public function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     $this->_metadata = null;
     if (!($this->_data = MFile::read($archive))) {
         $this->set('error.message', 'Unable to read archive');
         return MError::raiseWarning(100, $this->get('error.message'));
     }
     if (!$this->_getTarInfo($this->_data)) {
         return MError::raiseWarning(100, $this->get('error.message'));
     }
     for ($i = 0, $n = count($this->_metadata); $i < $n; $i++) {
         $type = strtolower($this->_metadata[$i]['type']);
         if ($type == 'file' || $type == 'unix file') {
             $buffer = $this->_metadata[$i]['data'];
             $path = MPath::clean($destination . '/' . $this->_metadata[$i]['name']);
             // Make sure the destination folder exists
             if (!MFolder::create(dirname($path))) {
                 $this->set('error.message', 'Unable to create destination');
                 return MError::raiseWarning(100, $this->get('error.message'));
             }
             if (MFile::write($path, $buffer) === false) {
                 $this->set('error.message', 'Unable to write entry');
                 return MError::raiseWarning(100, $this->get('error.message'));
             }
         }
     }
     return true;
 }
Example #2
0
 private function _extract($archive, $destination, $options)
 {
     // Initialise variables.
     $this->_data = null;
     $this->_metadata = null;
     if (!extension_loaded('zlib')) {
         $this->set('error.message', MText::_('MLIB_FILESYSTEM_ZIP_NOT_SUPPORTED'));
         return false;
     }
     if (!($this->_data = MFile::read($archive))) {
         $this->set('error.message', MText::_('MLIB_FILESYSTEM_ZIP_UNABLE_TO_READ'));
         return false;
     }
     if (!$this->_readZipInfo($this->_data)) {
         $this->set('error.message', MText::_('MLIB_FILESYSTEM_ZIP_INFO_FAILED'));
         return false;
     }
     for ($i = 0, $n = count($this->_metadata); $i < $n; $i++) {
         $lastPathCharacter = substr($this->_metadata[$i]['name'], -1, 1);
         if ($lastPathCharacter !== '/' && $lastPathCharacter !== '\\') {
             $buffer = $this->_getFileData($i);
             $path = MPath::clean($destination . '/' . $this->_metadata[$i]['name']);
             // Make sure the destination folder exists
             if (!MFolder::create(dirname($path))) {
                 $this->set('error.message', MText::_('MLIB_FILESYSTEM_ZIP_UNABLE_TO_CREATE_DESTINATION'));
                 return false;
             }
             if (MFile::write($path, $buffer) === false) {
                 $this->set('error.message', MText::_('MLIB_FILESYSTEM_ZIP_UNABLE_TO_WRITE_ENTRY'));
                 return false;
             }
         }
     }
     return true;
 }
Example #3
0
 public function extract($archive, $destination, $options = array())
 {
     // Initialise variables.
     $this->_data = null;
     if (!extension_loaded('bz2')) {
         $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_NOT_SUPPORTED'));
         return MError::raiseWarning(100, $this->get('error.message'));
     }
     if (!isset($options['use_streams']) || $options['use_streams'] == false) {
         // Old style: read the whole file and then parse it
         if (!($this->_data = MFile::read($archive))) {
             $this->set('error.message', 'Unable to read archive');
             return MError::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 MError::raiseWarning(100, $this->get('error.message'));
         }
         if (MFile::write($destination, $buffer) === false) {
             $this->set('error.message', 'Unable to write archive');
             return MError::raiseWarning(100, $this->get('error.message'));
         }
     } else {
         // New style! streams!
         $input = MFactory::getStream();
         $input->set('processingmethod', 'bz');
         // use bzip
         if (!$input->open($archive)) {
             $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_UNABLE_TO_READ'));
             return MError::raiseWarning(100, $this->get('error.message'));
         }
         $output = MFactory::getStream();
         if (!$output->open($destination, 'w')) {
             $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE'));
             $input->close();
             // close the previous file
             return MError::raiseWarning(100, $this->get('error.message'));
         }
         do {
             $this->_data = $input->read($input->get('chunksize', 8196));
             if ($this->_data) {
                 if (!$output->write($this->_data)) {
                     $this->set('error.message', MText::_('MLIB_FILESYSTEM_BZIP_UNABLE_TO_WRITE_FILE'));
                     return MError::raiseWarning(100, $this->get('error.message'));
                 }
             }
         } while ($this->_data);
         $output->close();
         $input->close();
     }
     return true;
 }
Example #4
0
 public function loadFile($file, $format = 'JSON', $options = array())
 {
     // Get the contents of the file
     mimport('framework.filesystem.file');
     $data = MFile::read($file);
     return $this->loadString($data, $format, $options);
 }