NtfsExtraField.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. namespace PhpZip\Extra\Fields;
  3. use PhpZip\Extra\ExtraField;
  4. use PhpZip\Util\PackUtil;
  5. /**
  6. * NTFS Extra Field.
  7. *
  8. * @see https://pkware.cachefly.net/webdocs/casestudies/APPNOTE.TXT .ZIP File Format Specification
  9. *
  10. * @author Ne-Lexa alexey@nelexa.ru
  11. * @license MIT
  12. */
  13. class NtfsExtraField implements ExtraField
  14. {
  15. /**
  16. * Modify time.
  17. *
  18. * @var int Unix Timestamp
  19. */
  20. private $mtime;
  21. /**
  22. * Access Time.
  23. *
  24. * @var int Unix Timestamp
  25. */
  26. private $atime;
  27. /**
  28. * Create Time.
  29. *
  30. * @var int Unix Time
  31. */
  32. private $ctime;
  33. /**
  34. * Returns the Header ID (type) of this Extra Field.
  35. * The Header ID is an unsigned short integer (two bytes)
  36. * which must be constant during the life cycle of this object.
  37. *
  38. * @return int
  39. */
  40. public static function getHeaderId()
  41. {
  42. return 0x000a;
  43. }
  44. /**
  45. * Initializes this Extra Field by deserializing a Data Block.
  46. *
  47. * @param string $data
  48. */
  49. public function deserialize($data)
  50. {
  51. $unpack = unpack('vtag/vsizeAttr', substr($data, 0, 4));
  52. if ($unpack['sizeAttr'] === 24) {
  53. $tagData = substr($data, 4, $unpack['sizeAttr']);
  54. $this->mtime = PackUtil::unpackLongLE(substr($tagData, 0, 8)) / 10000000 - 11644473600;
  55. $this->atime = PackUtil::unpackLongLE(substr($tagData, 8, 8)) / 10000000 - 11644473600;
  56. $this->ctime = PackUtil::unpackLongLE(substr($tagData, 16, 8)) / 10000000 - 11644473600;
  57. }
  58. }
  59. /**
  60. * Serializes a Data Block.
  61. *
  62. * @return string
  63. */
  64. public function serialize()
  65. {
  66. $serialize = '';
  67. if ($this->mtime !== null && $this->atime !== null && $this->ctime !== null) {
  68. $mtimeLong = ($this->mtime + 11644473600) * 10000000;
  69. $atimeLong = ($this->atime + 11644473600) * 10000000;
  70. $ctimeLong = ($this->ctime + 11644473600) * 10000000;
  71. $serialize .= pack('Vvv', 0, 1, 8 * 3)
  72. . PackUtil::packLongLE($mtimeLong)
  73. . PackUtil::packLongLE($atimeLong)
  74. . PackUtil::packLongLE($ctimeLong);
  75. }
  76. return $serialize;
  77. }
  78. /**
  79. * @return int
  80. */
  81. public function getMtime()
  82. {
  83. return $this->mtime;
  84. }
  85. /**
  86. * @param int $mtime
  87. */
  88. public function setMtime($mtime)
  89. {
  90. $this->mtime = (int) $mtime;
  91. }
  92. /**
  93. * @return int
  94. */
  95. public function getAtime()
  96. {
  97. return $this->atime;
  98. }
  99. /**
  100. * @param int $atime
  101. */
  102. public function setAtime($atime)
  103. {
  104. $this->atime = (int) $atime;
  105. }
  106. /**
  107. * @return int
  108. */
  109. public function getCtime()
  110. {
  111. return $this->ctime;
  112. }
  113. /**
  114. * @param int $ctime
  115. */
  116. public function setCtime($ctime)
  117. {
  118. $this->ctime = (int) $ctime;
  119. }
  120. }