/** * Read a file entry from the file. * * @param int $fileBinaryOffset The offset in the file to start reading from. * * @return FileEntry * * @throws \RuntimeException When the file name length in the dictionary is zero (corrupted dictionary). */ private function readFile($fileBinaryOffset) { $filenameLength = $this->file->readUint32le(); if (0 === $filenameLength) { throw new \RuntimeException('Unnamed file found.'); } $filename = $this->file->read($filenameLength); $fileSizeUncompressed = $this->file->readUint32le(); $timestamp = new \DateTime('@' . $this->file->readUint32le()); $fileSizeCompressed = $this->file->readUint32le(); $crc = $this->file->readUint32le(); $flags = $this->file->readUint32le(); if (0 < ($fileMetadataLength = $this->file->readUint32le())) { $metadata = unserialize($this->file->read($fileMetadataLength)); } $this->file->savePosition(); $this->file->seek($fileBinaryOffset); $content = $this->file->read($fileSizeCompressed); $this->file->loadPosition(); $file = FileEntry::createFromPhar($filename, $fileSizeUncompressed, $fileSizeCompressed, $timestamp, $crc, $flags, isset($metadata) ? $metadata : null, $content); // Ensure the crc is correct and the data can be decompressed. $file->getContent(); $this->phar->addFile($file); return $file; }
/** * Build the manifest and return it. * * @return void */ private function buildManifest() { $files = $this->phar->getFiles(); $alias = $this->phar->getAlias(); $metaData = $this->phar->getMetadata(); if (!empty($metaData)) { $metaData = serialize($metaData); } $manifest = new StreamWriter('php://temp'); $manifest->writeUint32le(count($files))->writeUint16be($this->phar->getApiVersion())->writeUint32le($this->phar->getFlags())->writeUint32le(strlen($alias))->write($alias)->writeUint32le(strlen($metaData))->write($metaData); // Add files now. foreach ($files as $file) { $manifest->writeStream($this->serializeFile($file)); } $this->file->writeUint32le($manifest->getLength()); $this->file->writeStream($manifest->seek(0)); }