| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- <?php
- namespace PhpZip\Model\Data;
- use PhpZip\Exception\ZipException;
- use PhpZip\Model\ZipData;
- /**
- * Class ZipFileData.
- */
- class ZipFileData implements ZipData
- {
- /** @var \SplFileInfo */
- private $file;
- /**
- * ZipStringData constructor.
- *
- * @param \SplFileInfo $fileInfo
- *
- * @throws ZipException
- */
- public function __construct(\SplFileInfo $fileInfo)
- {
- if (!$fileInfo->isFile()) {
- throw new ZipException('$fileInfo is not a file.');
- }
- if (!$fileInfo->isReadable()) {
- throw new ZipException('$fileInfo is not readable.');
- }
- $this->file = $fileInfo;
- }
- /**
- * @throws ZipException
- *
- * @return resource returns stream data
- */
- public function getDataAsStream()
- {
- if (!$this->file->isReadable()) {
- throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname()));
- }
- return fopen($this->file->getPathname(), 'rb');
- }
- /**
- * @throws ZipException
- *
- * @return string returns data as string
- */
- public function getDataAsString()
- {
- if (!$this->file->isReadable()) {
- throw new ZipException(sprintf('The %s file is no longer readable.', $this->file->getPathname()));
- }
- return file_get_contents($this->file->getPathname());
- }
- /**
- * @param resource $outStream
- *
- * @throws ZipException
- */
- public function copyDataToStream($outStream)
- {
- try {
- $stream = $this->getDataAsStream();
- stream_copy_to_stream($stream, $outStream);
- } finally {
- fclose($stream);
- }
- }
- }
|