ZipNewEntry.php 2.0 KB

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