Ejemplo n.º 1
0
 /**
  * Extract a Bzip2 compressed file to a given path
  *
  * @param   string  $archive      Path to Bzip2 archive to extract
  * @param   string  $destination  Path to extract archive to
  *
  * @return  boolean  True if successful
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public function extract($archive, $destination)
 {
     $this->data = null;
     if (!isset($this->options['use_streams']) || $this->options['use_streams'] == false) {
         // Old style: read the whole file and then parse it
         $this->data = file_get_contents($archive);
         if (!$this->data) {
             throw new \RuntimeException('Unable to read archive');
         }
         $buffer = bzdecompress($this->data);
         unset($this->data);
         if (empty($buffer)) {
             throw new \RuntimeException('Unable to decompress data');
         }
         if (File::write($destination, $buffer) === false) {
             throw new \RuntimeException('Unable to write archive');
         }
     } else {
         // New style! streams!
         $input = Stream::getStream();
         // Use bzip
         $input->set('processingmethod', 'bz');
         if (!$input->open($archive)) {
             throw new \RuntimeException('Unable to read archive (bz2)');
         }
         $output = Stream::getStream();
         if (!$output->open($destination, 'w')) {
             $input->close();
             throw new \RuntimeException('Unable to write archive (bz2)');
         }
         do {
             $this->data = $input->read($input->get('chunksize', 8196));
             if ($this->data) {
                 if (!$output->write($this->data)) {
                     $input->close();
                     throw new \RuntimeException('Unable to write archive (bz2)');
                 }
             }
         } while ($this->data);
         $output->close();
         $input->close();
     }
     return true;
 }
Ejemplo n.º 2
0
 /**
  * Moves a folder.
  *
  * @param   string   $src          The path to the source folder.
  * @param   string   $dest         The path to the destination folder.
  * @param   string   $path         An optional base path to prefix to the file names.
  * @param   boolean  $use_streams  Optionally use streams.
  *
  * @return  mixed  Error message on false or boolean true on success.
  *
  * @since   1.0
  */
 public static function move($src, $dest, $path = '', $use_streams = false)
 {
     if ($path) {
         $src = Path::clean($path . '/' . $src);
         $dest = Path::clean($path . '/' . $dest);
     }
     if (!is_dir(Path::clean($src))) {
         return 'Cannot find source folder';
     }
     if (is_dir(Path::clean($dest))) {
         return 'Folder already exists';
     }
     if ($use_streams) {
         $stream = Stream::getStream();
         if (!$stream->move($src, $dest)) {
             return 'Rename failed: ' . $stream->getError();
         }
         return true;
     } else {
         if (!@rename($src, $dest)) {
             return 'Rename failed';
         }
         return true;
     }
 }
Ejemplo n.º 3
0
 /**
  * Moves an uploaded file to a destination folder
  *
  * @param   string   $src          The name of the php (temporary) uploaded file
  * @param   string   $dest         The path (including filename) to move the uploaded file to
  * @param   boolean  $use_streams  True to use streams
  *
  * @return  boolean  True on success
  *
  * @since   1.0
  * @throws  FilesystemException
  */
 public static function upload($src, $dest, $use_streams = false)
 {
     // Ensure that the path is valid and clean
     $dest = Path::clean($dest);
     // Create the destination directory if it does not exist
     $baseDir = dirname($dest);
     if (!file_exists($baseDir)) {
         Folder::create($baseDir);
     }
     if ($use_streams) {
         $stream = Stream::getStream();
         if (!$stream->upload($src, $dest)) {
             throw new FilesystemException(__METHOD__ . ': ' . $stream->getError());
         }
         return true;
     } else {
         if (is_writeable($baseDir) && move_uploaded_file($src, $dest)) {
             // Short circuit to prevent file permission errors
             if (Path::setPermissions($dest)) {
                 return true;
             } else {
                 throw new FilesystemException(__METHOD__ . ': Failed to change file permissions.');
             }
         } else {
             throw new FilesystemException(__METHOD__ . ': Failed to move file.');
         }
         return false;
     }
 }
Ejemplo n.º 4
0
 /**
  * Tests getStream()
  *
  * @return  void
  */
 public function testGetStream()
 {
     $this->object = Stream::getStream();
     $this->assertInstanceOf('Joomla\\Filesystem\\Stream', $this->object, 'getStream must return an instance of Joomla\\Filesystem\\Stream');
 }
Ejemplo n.º 5
0
 /**
  * Extract a Gzip compressed file to a given path
  *
  * @param   string  $archive      Path to ZIP archive to extract
  * @param   string  $destination  Path to extract archive to
  *
  * @return  boolean  True if successful
  *
  * @since   1.0
  * @throws  \RuntimeException
  */
 public function extract($archive, $destination)
 {
     $this->data = null;
     if (!isset($this->options['use_streams']) || $this->options['use_streams'] == false) {
         $this->data = file_get_contents($archive);
         if (!$this->data) {
             throw new \RuntimeException('Unable to read archive');
         }
         $position = $this->getFilePosition();
         $buffer = gzinflate(substr($this->data, $position, strlen($this->data) - $position));
         if (empty($buffer)) {
             throw new \RuntimeException('Unable to decompress data');
         }
         if (File::write($destination, $buffer) === false) {
             throw new \RuntimeException('Unable to write archive');
         }
     } else {
         // New style! streams!
         $input = Stream::getStream();
         // Use gz
         $input->set('processingmethod', 'gz');
         if (!$input->open($archive)) {
             throw new \RuntimeException('Unable to read archive (gz)');
         }
         $output = Stream::getStream();
         if (!$output->open($destination, 'w')) {
             $input->close();
             throw new \RuntimeException('Unable to write archive (gz)');
         }
         do {
             $this->data = $input->read($input->get('chunksize', 8196));
             if ($this->data) {
                 if (!$output->write($this->data)) {
                     $input->close();
                     throw new \RuntimeException('Unable to write file (gz)');
                 }
             }
         } while ($this->data);
         $output->close();
         $input->close();
     }
     return true;
 }