ZipNewEntry.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace PhpZip\Model\Entry;
  3. use PhpZip\Exception\InvalidArgumentException;
  4. use PhpZip\Exception\ZipException;
  5. use PhpZip\ZipFileInterface;
  6. /**
  7. * Abstract class for new zip entry.
  8. *
  9. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  10. * @author Ne-Lexa alexey@nelexa.ru
  11. * @license MIT
  12. */
  13. class ZipNewEntry extends ZipAbstractEntry
  14. {
  15. /**
  16. * @var resource|string|null
  17. */
  18. protected $content;
  19. /**
  20. * @var bool
  21. */
  22. private $clone = false;
  23. /**
  24. * ZipNewEntry constructor.
  25. * @param string|resource|null $content
  26. * @throws InvalidArgumentException
  27. */
  28. public function __construct($content = null)
  29. {
  30. parent::__construct();
  31. if ($content !== null && !is_string($content) && !is_resource($content)) {
  32. throw new InvalidArgumentException('invalid content');
  33. }
  34. $this->content = $content;
  35. }
  36. /**
  37. * Returns an string content of the given entry.
  38. *
  39. * @return null|string
  40. * @throws ZipException
  41. */
  42. public function getEntryContent()
  43. {
  44. if (is_resource($this->content)) {
  45. return stream_get_contents($this->content, -1, 0);
  46. }
  47. return $this->content;
  48. }
  49. /**
  50. * Version needed to extract.
  51. *
  52. * @return int
  53. */
  54. public function getVersionNeededToExtract()
  55. {
  56. $method = $this->getMethod();
  57. return self::METHOD_WINZIP_AES === $method ? 51 :
  58. (
  59. ZipFileInterface::METHOD_BZIP2 === $method ? 46 :
  60. (
  61. $this->isZip64ExtensionsRequired() ? 45 :
  62. (ZipFileInterface::METHOD_DEFLATED === $method || $this->isDirectory() ? 20 : 10)
  63. )
  64. );
  65. }
  66. /**
  67. * Clone extra fields
  68. */
  69. public function __clone()
  70. {
  71. $this->clone = true;
  72. parent::__clone();
  73. }
  74. public function __destruct()
  75. {
  76. if (!$this->clone && null !== $this->content && is_resource($this->content)) {
  77. fclose($this->content);
  78. $this->content = null;
  79. }
  80. }
  81. }