/titania/includes/library/ezcomponents/Archive/zip/headers/central_directory_end.php

https://github.com/TheDgtl/customisation-db · PHP · 237 lines · 103 code · 18 blank · 116 comment · 4 complexity · 5eddb7c6407cfd3a6957e53ee0338e95 MD5 · raw file

  1. <?php
  2. /**
  3. * File containing the ezcArchiveCentralDirectoryHeader class.
  4. *
  5. * @package Archive
  6. * @version //autogentag//
  7. * @copyright Copyright (C) 2005-2010 eZ Systems AS. All rights reserved.
  8. * @license http://ez.no/licenses/new_bsd New BSD License
  9. * @access private
  10. */
  11. /**
  12. * The ezcArchiveCentralDirectoryEndHeader class represents the Zip central directory end header.
  13. *
  14. * ezcArchiveCentralDirectoryEndHeader can read the header from an ezcArchiveCharacterFile.
  15. *
  16. * The values from the headers are directly accessible via the class properties, and allows
  17. * reading and writing to specific header values.
  18. *
  19. * The entire header can be appended to an ezcArchiveCharacterFile again.
  20. *
  21. * The central directory end header is the last header of the ZIP archive. Some algorithms
  22. * search back to this header in order to find the other headers. The variable comment length
  23. * makes it tricky.
  24. *
  25. * The Central Directory End Header has the following structure:
  26. *
  27. * <pre>
  28. * + ---+---------+------------+------------------------------------+-------------------------------------------------------------------------------+
  29. * | ID | Offset | Field size | Property | Description |
  30. * +----+---------+------------+------------------------------------+-------------------------------------------------------------------------------+
  31. * | | 0 | 4 | - | Central directory header signature |
  32. * | | 4 | 2 | diskNumber | nr of this disk |
  33. * | | 6 | 2 | centralDirectoryDisk | number of the disk with the start of the central directory |
  34. * | | 8 | 2 | totalCentralDirectoryEntriesOnDisk | total number of entries in the central directory on this disk |
  35. * | | 10 | 2 | totalCentralDirectoryEntries | total number of entries in the central directory |
  36. * | | 12 | 4 | centralDirectorySize | size of the central directory |
  37. * | | 16 | 4 | centralDirectoryStart | offset of start of central directory with respect to the starting disk number |
  38. * | X: | 20 | 2 | commentLength | .ZIP file comment length |
  39. * | | 22 | X | comment | .ZIP file comment |
  40. * +----+---------+------------+------------------------------------+-------------------------------------------------------------------------------+
  41. * </pre>
  42. *
  43. *
  44. * The columns of the table are:
  45. * - ID gives a label to a specific field or row in the table.
  46. * - Offset describes the start position of a header field.
  47. * - Field size describes the size of the field in bytes.
  48. * - Property is the name of the property that will be set by the header field.
  49. * - Description explains what this field describes.
  50. *
  51. *
  52. * @package Archive
  53. * @version //autogentag//
  54. * @access private
  55. */
  56. class ezcArchiveCentralDirectoryEndHeader
  57. {
  58. /**
  59. * Defines the signature of this header.
  60. */
  61. const magic = 0x06054b50;
  62. /**
  63. * Holds the properties of this class.
  64. *
  65. * @var array(string=>mixed)
  66. */
  67. private $properties = array();
  68. /**
  69. * Creates and initializes a new header.
  70. *
  71. * If the ezcArchiveCharacterFile $file is null then the header will be empty.
  72. * When an ezcArchiveCharacterFile is given, the file position should be directly after the
  73. * signature of the header. This header will be read from the file and initialized in this class.
  74. *
  75. * @param ezcArchiveCharacterFile $file
  76. */
  77. public function __construct( ezcArchiveCharacterFile $file = null )
  78. {
  79. if ( !is_null( $file ) )
  80. {
  81. $this->properties = unpack (
  82. "vdiskNumber/".
  83. "vcentralDirectoryDisk/".
  84. "vtotalCentralDirectoryEntriesOnDisk/".
  85. "vtotalCentralDirectoryEntries/".
  86. "VcentralDirectorySize/".
  87. "VcentralDirectoryStart/".
  88. "vcommentLength",
  89. $file->read( 18 ) );
  90. $this->properties["comment"] = $file->read( $this->properties["commentLength"] );
  91. }
  92. else
  93. {
  94. $this->properties["diskNumber"] = 0;
  95. $this->properties["centralDirectoryDisk"] = 0;
  96. $this->properties["totalCentralDirectoryEntries"] = 0;
  97. $this->properties["totalCentralDirectoryEntriesOnDisk"] = 0;
  98. $this->properties["totalCentralDirectorySize"] = 0;
  99. $this->properties["totalCentralDirectoryStart"] = 0;
  100. $this->setComment( "" );
  101. }
  102. }
  103. /**
  104. * Sets the property $name to $value.
  105. *
  106. * @throws ezcBasePropertyNotFoundException if the property does not exist.
  107. * @throws ezcBasePropertyReadOnlyException if the property is read-only
  108. * @param string $name
  109. * @param mixed $value
  110. * @return void
  111. * @ignore
  112. */
  113. public function __set( $name, $value )
  114. {
  115. switch ( $name )
  116. {
  117. case "diskNumber":
  118. case "centralDirectoryDisk":
  119. case "totalCentralDirectoryEntriesOnDisk":
  120. throw new ezcBasePropertyReadOnlyException( $name );
  121. case "totalCentralDirectoryEntries":
  122. $this->setTotalDirectoryEntries( $value );
  123. break;
  124. case "centralDirectorySize":
  125. case "centralDirectoryStart":
  126. $this->properties[$name] = $value;
  127. break;
  128. case "commentLength":
  129. throw new ezcBasePropertyReadOnlyException( $name );
  130. case "comment":
  131. $this->setComment( $value );
  132. break;
  133. default:
  134. throw new ezcBasePropertyNotFoundException( $name );
  135. }
  136. }
  137. /**
  138. * Returns the value of the property $name.
  139. *
  140. * @throws ezcBasePropertyNotFoundException if the property does not exist.
  141. * @param string $name
  142. * @return mixed
  143. * @ignore
  144. */
  145. public function __get( $name )
  146. {
  147. switch ( $name )
  148. {
  149. case "diskNumber":
  150. case "centralDirectoryDisk":
  151. case "totalCentralDirectoryEntriesOnDisk":
  152. case "totalCentralDirectoryEntries":
  153. case "centralDirectorySize":
  154. case "centralDirectoryStart":
  155. case "commentLength":
  156. case "comment":
  157. return $this->properties[$name];
  158. default:
  159. throw new ezcBasePropertyNotFoundException( $name );
  160. }
  161. }
  162. /**
  163. * Sets the comment and comment length in the header from the string $comment.
  164. *
  165. * @param string $comment
  166. * @return void
  167. */
  168. public function setComment( $comment )
  169. {
  170. $this->properties["comment"] = $comment;
  171. $this->properties["commentLength"] = strlen( $comment );
  172. }
  173. /**
  174. * Sets the total directory entries to the int $numberOfEntries.
  175. *
  176. * The properties: diskNumber and centralDirectory will be set to 0.
  177. * The properties: totalCentralDirectoryEntriesOnDisk and totalCentralDirectoryEntries are set to the $numberOfEntries.
  178. *
  179. * @param int $numberOfEntries
  180. * @return void
  181. */
  182. public function setTotalDirectoryEntries( $numberOfEntries )
  183. {
  184. $this->properties["diskNumber"] = 0;
  185. $this->properties["centralDirectoryDisk"] = 0;
  186. $this->properties["totalCentralDirectoryEntriesOnDisk"] = $numberOfEntries;
  187. $this->properties["totalCentralDirectoryEntries"] = $numberOfEntries;
  188. }
  189. /**
  190. * Serializes this header and appends it to the given ezcArchiveCharacterFile $archiveFile.
  191. *
  192. * @param ezcArchiveCharacterFile $archiveFile
  193. * @return void
  194. */
  195. public function writeEncodedHeader( $archiveFile )
  196. {
  197. $enc = pack ( "VvvvvVVv",
  198. self::magic,
  199. $this->properties["diskNumber"],
  200. $this->properties["centralDirectoryDisk"],
  201. $this->properties["totalCentralDirectoryEntriesOnDisk"],
  202. $this->properties["totalCentralDirectoryEntries"],
  203. $this->properties["centralDirectorySize"],
  204. $this->properties["centralDirectoryStart"],
  205. $this->properties["commentLength"] );
  206. $archiveFile->write( $enc . $this->properties["comment"] );
  207. }
  208. /**
  209. * Returns true if the given string $string matches with the current signature.
  210. *
  211. * @param string $string
  212. * @return void
  213. */
  214. public static function isSignature( $string )
  215. {
  216. return $string == pack( "V", self::magic );
  217. }
  218. }
  219. ?>