PageRenderTime 44ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/Utilities/Compression/ZipConstants.cs

#
C# | 326 lines | 77 code | 57 blank | 192 comment | 0 complexity | cc39a95072dcbfec90cde7a2ec0c18e3 MD5 | raw file
Possible License(s): Apache-2.0
  1. // Based on Mike Krueger's SharpZipLib, Copyright (C) 2001 (GNU license).
  2. // Authors of the original java version: Jochen Hoenicke, John Leuner
  3. // See http://www.ISeeSharpCode.com for more information.
  4. using System.Text;
  5. namespace Delta.Utilities.Compression
  6. {
  7. /// <summary>
  8. /// This class contains constants used for Zip format files
  9. /// </summary>
  10. public sealed class ZipConstants
  11. {
  12. #region Constants
  13. /// <summary>
  14. /// The version made by field for entries in the central header when
  15. /// created by this library
  16. /// </summary>
  17. /// <remarks>
  18. /// This is also the Zip version for the library when comparing against
  19. /// the version required to extract for an entry. See
  20. /// <see cref="Streams.ZipInputStream.CanDecompressEntry">
  21. /// ZipInputStream.CanDecompressEntry</see>.
  22. /// </remarks>
  23. public const int VersionMadeBy = 20;
  24. /// <summary>
  25. /// The minimum version required to support strong encryption
  26. /// </summary>
  27. public const int VersionStrongEncryption = 50;
  28. // The local entry header
  29. /// <summary>
  30. /// Size of local entry header (excluding variable length fields at end)
  31. /// </summary>
  32. public const int LocalHeader = 30;
  33. /// <summary>
  34. /// Signature for local entry header
  35. /// </summary>
  36. public const int LocalSignature = 'P' | ('K' << 8) | (3 << 16) | (4 << 24);
  37. /// <summary>
  38. /// Offset of version to extract in local entry header
  39. /// </summary>
  40. public const int LocalVersion = 4;
  41. /// <summary>
  42. /// Offset of general purpose flags in local entry header
  43. /// </summary>
  44. public const int LocalFlag = 6;
  45. /// <summary>
  46. /// Offset of compression method in local entry header
  47. /// </summary>
  48. public const int LocalHow = 8;
  49. /// <summary>
  50. /// Offset of last mod file time + date in local entry header
  51. /// </summary>
  52. public const int LocalTime = 10;
  53. /// <summary>
  54. /// Offset of crc-32 in local entry header
  55. /// </summary>
  56. public const int LocalCrc = 14;
  57. /// <summary>
  58. /// Offset of compressed size in local entry header
  59. /// </summary>
  60. public const int LocalSize = 18;
  61. /// <summary>
  62. /// Offset of uncompressed size in local entry header
  63. /// </summary>
  64. public const int LocalLength = 22;
  65. /// <summary>
  66. /// Offset of file name length in local entry header
  67. /// </summary>
  68. public const int LocalName = 26;
  69. /// <summary>
  70. /// Offset of extra field length in local entry header
  71. /// </summary>
  72. public const int LocalExtension = 28;
  73. /// <summary>
  74. /// Signature for spanning entry
  75. /// </summary>
  76. public const int SpanningSig =
  77. 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
  78. /// <summary>
  79. /// Signature for temporary spanning entry
  80. /// </summary>
  81. public const int SpanningTempSig =
  82. 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24);
  83. /// <summary>
  84. /// Signature for data descriptor
  85. /// </summary>
  86. /// <remarks>
  87. /// This is only used where the length, Crc, or compressed size isn't
  88. /// known when the entry is created and the output stream doesnt support
  89. /// seeking. The local entry cannot be 'patched' with the correct values
  90. /// in this case so the values are recorded after the data prefixed by
  91. /// this header, as well as in the central directory.
  92. /// </remarks>
  93. public const int ExternSig =
  94. 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
  95. /// <summary>
  96. /// Size of data descriptor
  97. /// </summary>
  98. public const int ExternHeader = 16;
  99. /// <summary>
  100. /// Offset of crc-32 in data descriptor
  101. /// </summary>
  102. public const int ExternCrc = 4;
  103. /// <summary>
  104. /// Offset of compressed size in data descriptor
  105. /// </summary>
  106. public const int ExternSize = 8;
  107. /// <summary>
  108. /// Offset of uncompressed length in data descriptor
  109. /// </summary>
  110. public const int ExternLength = 12;
  111. /// <summary>
  112. /// Signature for central header
  113. /// </summary>
  114. public const int CentralDirectorySig =
  115. 'P' | ('K' << 8) | (1 << 16) | (2 << 24);
  116. /// <summary>
  117. /// Signature for Zip64 central file header
  118. /// </summary>
  119. public const int CentralDirectorySig64 =
  120. 'P' | ('K' << 8) | (6 << 16) | (6 << 24);
  121. /// <summary>
  122. /// Central header digitial signature
  123. /// </summary>
  124. public const int CentralDigitalSig =
  125. 'P' | ('K' << 8) | (5 << 16) | (5 << 24);
  126. /// <summary>
  127. /// Size of central header entry
  128. /// </summary>
  129. public const int CentralHeader = 46;
  130. /// <summary>
  131. /// Offset of version made by in central file header
  132. /// </summary>
  133. public const int CentralVem = 4;
  134. /// <summary>
  135. /// Offset of version needed to extract in central file header
  136. /// </summary>
  137. public const int CentralVersion = 6;
  138. /// <summary>
  139. /// Offset of general purpose bit flag in central file header
  140. /// </summary>
  141. public const int CentralFlag = 8;
  142. /// <summary>
  143. /// Offset of compression method in central file header
  144. /// </summary>
  145. public const int CentralHow = 10;
  146. /// <summary>
  147. /// Offset of time/date in central file header
  148. /// </summary>
  149. public const int CentralTime = 12;
  150. /// <summary>
  151. /// Offset of crc-32 in central file header
  152. /// </summary>
  153. public const int CentralCrc = 16;
  154. /// <summary>
  155. /// Offset of compressed size in central file header
  156. /// </summary>
  157. public const int CentralSize = 20;
  158. /// <summary>
  159. /// Offset of uncompressed size in central file header
  160. /// </summary>
  161. public const int CentralLength = 24;
  162. /// <summary>
  163. /// Offset of file name length in central file header
  164. /// </summary>
  165. public const int CentralName = 28;
  166. /// <summary>
  167. /// Offset of extra field length in central file header
  168. /// </summary>
  169. public const int CentralExtension = 30;
  170. /// <summary>
  171. /// Offset of file comment length in central file header
  172. /// </summary>
  173. public const int CentralComment = 32;
  174. /// <summary>
  175. /// Offset of disk start number in central file header
  176. /// </summary>
  177. public const int CentralDiskStartNumber = 34;
  178. /// <summary>
  179. /// Offset of internal file attributes in central file header
  180. /// </summary>
  181. public const int CentralAttributes = 36;
  182. /// <summary>
  183. /// Offset of external file attributes in central file header
  184. /// </summary>
  185. public const int CentralExternalAttributes = 38;
  186. /// <summary>
  187. /// Offset of relative offset of local header in central file header
  188. /// </summary>
  189. public const int CentralOffset = 42;
  190. // The entries at the end of central directory
  191. /// <summary>
  192. /// End of central directory record signature
  193. /// </summary>
  194. public const int EndSig =
  195. 'P' | ('K' << 8) | (5 << 16) | (6 << 24);
  196. /// <summary>
  197. /// Size of end of central record (excluding variable fields)
  198. /// </summary>
  199. public const int EndHeader = 22;
  200. // The following two fields are missing in SUN JDK
  201. /// <summary>
  202. /// Offset of number of this disk
  203. /// </summary>
  204. public const int EndNumberOfDisk = 4;
  205. /// <summary>
  206. /// Offset of number of disk with start of central directory
  207. /// </summary>
  208. public const int EndNumberOfDiskCentralDirectory = 6;
  209. /// <summary>
  210. /// Offset of number of entries in the central directory of this disk
  211. /// </summary>
  212. public const int EndEntriesInCentralDirectory = 8;
  213. /// <summary>
  214. /// Offset of total number of entries in the central directory
  215. /// </summary>
  216. public const int EndTotalNumberOfEntriesInCentralDirectory = 10;
  217. /// <summary>
  218. /// Offset of size of central directory
  219. /// </summary>
  220. public const int EndSizeOfCentralDirectory = 12;
  221. /// <summary>
  222. /// Offset of offset of start of central directory with respect to
  223. /// starting disk number
  224. /// </summary>
  225. public const int EndOffset = 16;
  226. /// <summary>
  227. /// Offset of ZIP file comment length
  228. /// </summary>
  229. public const int EndComment = 20;
  230. /// <summary>
  231. /// Size of cryptographic header stored before entry data
  232. /// </summary>
  233. public const int CryptoHeaderSize = 12;
  234. #endregion
  235. #region Public
  236. #endregion
  237. #region Convert methods
  238. /// <summary>
  239. /// Convert a portion of a byte array to a string.
  240. /// </summary>
  241. /// <param name="data">
  242. /// Data to convert to string
  243. /// </param>
  244. /// <param name="length">
  245. /// Number of bytes to convert starting from index 0
  246. /// </param>
  247. /// <returns>
  248. /// data[0]..data[length - 1] converted to a string
  249. /// </returns>
  250. public static string ConvertToString(byte[] data, int length)
  251. {
  252. return Encoding.ASCII.GetString(data, 0, length);
  253. }
  254. /// <summary>
  255. /// Convert byte array to string
  256. /// </summary>
  257. /// <param name="data">
  258. /// Byte array to convert
  259. /// </param>
  260. /// <returns>
  261. /// <paramref name="data">data</paramref>converted to a string
  262. /// </returns>
  263. public static string ConvertToString(byte[] data)
  264. {
  265. return ConvertToString(data, data.Length);
  266. }
  267. #endregion
  268. }
  269. }