/** * Extract a ZIP compressed file to a given path * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * * @return boolean True if successful * @throws Exception */ public function extract($archive, $destination) { $this->data = null; $this->metadata = null; $this->data = file_get_contents($archive); if (!$this->data) { throw new Exception('Unable to read archive'); } $this->getTarInfo($this->data); 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 = xapp_Path2::clean($destination . '/' . $this->metadata[$i]['name']); // Make sure the destination folder exists if (!xapp_Folder2::create(dirname($path))) { throw new Exception('Unable to create destination'); } if (xapp_File2::write($path, $buffer) === false) { throw new Exception('Unable to write entry'); } } } return true; }
/** * 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 Exception */ public function extract($archive, $destination) { $this->data = null; $this->options['use_streams'] = false; 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 Exception('Unable to read archive'); } $buffer = bzdecompress($this->data); unset($this->data); if (empty($buffer)) { throw new Exception('Unable to decompress data'); } if (xapp_File2::write($destination, $buffer) === false) { throw new Exception('Unable to write archive'); } } else { // New style! streams! $input = Stream::getStream(); // Use bzip $input->set('processingmethod', 'bz'); if (!$input->open($archive)) { throw new Exception('Unable to read archive (bz2)'); } $output = Stream::getStream(); if (!$output->open($destination, 'w')) { $input->close(); throw new Exception('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 Exception('Unable to write archive (bz2)'); } } } while ($this->data); $output->close(); $input->close(); } // @codeCoverageIgnoreEnd return true; }
/** * 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 Exception */ public function extract($archive, $destination) { $this->data = null; $this->options['use_streams'] = false; if (!isset($this->options['use_streams']) || $this->options['use_streams'] == false) { $this->data = file_get_contents($archive); if (!$this->data) { throw new Exception('Unable to read archive'); } $position = $this->getFilePosition(); $buffer = gzinflate(substr($this->data, $position, strlen($this->data) - $position)); if (empty($buffer)) { throw new Exception('Unable to decompress data'); } if (xapp_File2::write($destination, $buffer) === false) { throw new Exception('Unable to write archive'); } } else { // New style! streams! $input = Stream::getStream(); // Use gz $input->set('processingmethod', 'gz'); if (!$input->open($archive)) { throw new Exception('Unable to read archive (gz)'); } $output = Stream::getStream(); if (!$output->open($destination, 'w')) { $input->close(); throw new Exception('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 Exception('Unable to write file (gz)'); } } } while ($this->data); $output->close(); $input->close(); } // @codeCoverageIgnoreEnd return true; }
/** * Extract a ZIP compressed file to a given path using native php api calls for speed * * @param string $archive Path to ZIP archive to extract * @param string $destination Path to extract archive into * * @return boolean True on success * * @since 1.0 * @throws \RuntimeException */ protected function extractNative($archive, $destination) { $zip = zip_open($archive); if (is_resource($zip)) { // Make sure the destination folder exists if (!xapp_Folder2::create($destination)) { throw new Exception('Unable to create destination'); } // Read files in the archive while ($file = @zip_read($zip)) { if (zip_entry_open($zip, $file, "r")) { if (substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/") { $buffer = zip_entry_read($file, zip_entry_filesize($file)); if (xapp_File2::write($destination . '/' . zip_entry_name($file), $buffer) === false) { throw new Exception('Unable to write entry'); } zip_entry_close($file); } } else { throw new Exception('Unable to read entry'); } } @zip_close($zip); } else { throw new \RuntimeException('Unable to open archive'); } return true; }