ZipNewStreamEntry.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace PhpZip\Model\Entry;
  3. use PhpZip\Exception\InvalidArgumentException;
  4. use PhpZip\Exception\ZipException;
  5. /**
  6. * New zip entry from stream.
  7. *
  8. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  9. * @author Ne-Lexa alexey@nelexa.ru
  10. * @license MIT
  11. */
  12. class ZipNewStreamEntry extends ZipNewEntry
  13. {
  14. /**
  15. * @var resource
  16. */
  17. private $stream;
  18. /**
  19. * ZipNewStreamEntry constructor.
  20. * @param resource $stream
  21. * @throws InvalidArgumentException
  22. */
  23. public function __construct($stream)
  24. {
  25. if (!is_resource($stream)) {
  26. throw new InvalidArgumentException('stream is not resource');
  27. }
  28. $this->stream = $stream;
  29. }
  30. /**
  31. * Returns an string content of the given entry.
  32. *
  33. * @return null|string
  34. * @throws ZipException
  35. */
  36. public function getEntryContent()
  37. {
  38. return stream_get_contents($this->stream, -1, 0);
  39. }
  40. /**
  41. * Release stream resource.
  42. */
  43. function __destruct()
  44. {
  45. if (null !== $this->stream) {
  46. fclose($this->stream);
  47. $this->stream = null;
  48. }
  49. }
  50. }