/** * 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 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; }
/** * Write contents to a file * * @param string $file The full file path * @param string &$buffer The buffer to write * @param boolean $use_streams Use streams * * @return boolean True on success * * @since 1.0 * @throws FilesystemException */ public static function write($file, &$buffer, $use_streams = false) { @set_time_limit(ini_get('max_execution_time')); // If the destination directory doesn't exist we need to create it if (!file_exists(dirname($file))) { xapp_Folder2::create(dirname($file)); } if ($use_streams) { $stream = Stream::getStream(); // Beef up the chunk size to a meg $stream->set('chunksize', 1024 * 1024); if (!$stream->writeFile($file, $buffer)) { throw new FilesystemException(sprintf('%1$s(%2$s): %3$s', __METHOD__, $file, $stream->getError())); } return true; } else { $file = xapp_Path2::clean($file); $ret = is_int(file_put_contents($file, $buffer)) ? true : false; return $ret; } }