ZipConstants.cs 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  1. using System;
  2. namespace System.Net.ApiService.Compression.Zip {
  3. #region Enumerations
  4. /// <summary>
  5. /// Determines how entries are tested to see if they should use Zip64 extensions or not.
  6. /// </summary>
  7. public enum UseZip64
  8. {
  9. /// <summary>
  10. /// Zip64 will not be forced on entries during processing.
  11. /// </summary>
  12. /// <remarks>An entry can have this overridden if required <see cref="ZipEntry.ForceZip64"></see></remarks>
  13. Off,
  14. /// <summary>
  15. /// Zip64 should always be used.
  16. /// </summary>
  17. On,
  18. /// <summary>
  19. /// #ZipLib will determine use based on entry values when added to archive.
  20. /// </summary>
  21. Dynamic,
  22. }
  23. /// <summary>
  24. /// The kind of compression used for an entry in an archive
  25. /// </summary>
  26. public enum CompressionMethod
  27. {
  28. /// <summary>
  29. /// A direct copy of the file contents is held in the archive
  30. /// </summary>
  31. Stored = 0,
  32. /// <summary>
  33. /// Common Zip compression method using a sliding dictionary
  34. /// of up to 32KB and secondary compression from Huffman/Shannon-Fano trees
  35. /// </summary>
  36. Deflated = 8,
  37. /// <summary>
  38. /// An extension to deflate with a 64KB window. Not supported by #Zip currently
  39. /// </summary>
  40. Deflate64 = 9,
  41. /// <summary>
  42. /// BZip2 compression. Not supported by #Zip.
  43. /// </summary>
  44. BZip2 = 12,
  45. /// <summary>
  46. /// LZMA compression. Not supported by #Zip.
  47. /// </summary>
  48. LZMA = 14,
  49. /// <summary>
  50. /// PPMd compression. Not supported by #Zip.
  51. /// </summary>
  52. PPMd = 98,
  53. /// <summary>
  54. /// WinZip special for AES encryption, Now supported by #Zip.
  55. /// </summary>
  56. WinZipAES = 99,
  57. }
  58. /// <summary>
  59. /// Identifies the encryption algorithm used for an entry
  60. /// </summary>
  61. public enum EncryptionAlgorithm
  62. {
  63. /// <summary>
  64. /// No encryption has been used.
  65. /// </summary>
  66. None = 0,
  67. /// <summary>
  68. /// Encrypted using PKZIP 2.0 or 'classic' encryption.
  69. /// </summary>
  70. PkzipClassic = 1,
  71. /// <summary>
  72. /// DES encryption has been used.
  73. /// </summary>
  74. Des = 0x6601,
  75. /// <summary>
  76. /// RC2 encryption has been used for encryption.
  77. /// </summary>
  78. RC2 = 0x6602,
  79. /// <summary>
  80. /// Triple DES encryption with 168 bit keys has been used for this entry.
  81. /// </summary>
  82. TripleDes168 = 0x6603,
  83. /// <summary>
  84. /// Triple DES with 112 bit keys has been used for this entry.
  85. /// </summary>
  86. TripleDes112 = 0x6609,
  87. /// <summary>
  88. /// AES 128 has been used for encryption.
  89. /// </summary>
  90. Aes128 = 0x660e,
  91. /// <summary>
  92. /// AES 192 has been used for encryption.
  93. /// </summary>
  94. Aes192 = 0x660f,
  95. /// <summary>
  96. /// AES 256 has been used for encryption.
  97. /// </summary>
  98. Aes256 = 0x6610,
  99. /// <summary>
  100. /// RC2 corrected has been used for encryption.
  101. /// </summary>
  102. RC2Corrected = 0x6702,
  103. /// <summary>
  104. /// Blowfish has been used for encryption.
  105. /// </summary>
  106. Blowfish = 0x6720,
  107. /// <summary>
  108. /// Twofish has been used for encryption.
  109. /// </summary>
  110. Twofish = 0x6721,
  111. /// <summary>
  112. /// RC4 has been used for encryption.
  113. /// </summary>
  114. RC4 = 0x6801,
  115. /// <summary>
  116. /// An unknown algorithm has been used for encryption.
  117. /// </summary>
  118. Unknown = 0xffff
  119. }
  120. /// <summary>
  121. /// Defines the contents of the general bit flags field for an archive entry.
  122. /// </summary>
  123. [Flags]
  124. public enum GeneralBitFlags
  125. {
  126. /// <summary>
  127. /// Bit 0 if set indicates that the file is encrypted
  128. /// </summary>
  129. Encrypted = 0x0001,
  130. /// <summary>
  131. /// Bits 1 and 2 - Two bits defining the compression method (only for Method 6 Imploding and 8,9 Deflating)
  132. /// </summary>
  133. Method = 0x0006,
  134. /// <summary>
  135. /// Bit 3 if set indicates a trailing data desciptor is appended to the entry data
  136. /// </summary>
  137. Descriptor = 0x0008,
  138. /// <summary>
  139. /// Bit 4 is reserved for use with method 8 for enhanced deflation
  140. /// </summary>
  141. ReservedPKware4 = 0x0010,
  142. /// <summary>
  143. /// Bit 5 if set indicates the file contains Pkzip compressed patched data.
  144. /// Requires version 2.7 or greater.
  145. /// </summary>
  146. Patched = 0x0020,
  147. /// <summary>
  148. /// Bit 6 if set indicates strong encryption has been used for this entry.
  149. /// </summary>
  150. StrongEncryption = 0x0040,
  151. /// <summary>
  152. /// Bit 7 is currently unused
  153. /// </summary>
  154. Unused7 = 0x0080,
  155. /// <summary>
  156. /// Bit 8 is currently unused
  157. /// </summary>
  158. Unused8 = 0x0100,
  159. /// <summary>
  160. /// Bit 9 is currently unused
  161. /// </summary>
  162. Unused9 = 0x0200,
  163. /// <summary>
  164. /// Bit 10 is currently unused
  165. /// </summary>
  166. Unused10 = 0x0400,
  167. /// <summary>
  168. /// Bit 11 if set indicates the filename and
  169. /// comment fields for this file must be encoded using UTF-8.
  170. /// </summary>
  171. UnicodeText = 0x0800,
  172. /// <summary>
  173. /// Bit 12 is documented as being reserved by PKware for enhanced compression.
  174. /// </summary>
  175. EnhancedCompress = 0x1000,
  176. /// <summary>
  177. /// Bit 13 if set indicates that values in the local header are masked to hide
  178. /// their actual values, and the central directory is encrypted.
  179. /// </summary>
  180. /// <remarks>
  181. /// Used when encrypting the central directory contents.
  182. /// </remarks>
  183. HeaderMasked = 0x2000,
  184. /// <summary>
  185. /// Bit 14 is documented as being reserved for use by PKware
  186. /// </summary>
  187. ReservedPkware14 = 0x4000,
  188. /// <summary>
  189. /// Bit 15 is documented as being reserved for use by PKware
  190. /// </summary>
  191. ReservedPkware15 = 0x8000
  192. }
  193. #endregion Enumerations
  194. /// <summary>
  195. /// This class contains constants used for Zip format files
  196. /// </summary>
  197. public static class ZipConstants
  198. {
  199. #region Versions
  200. /// <summary>
  201. /// The version made by field for entries in the central header when created by this library
  202. /// </summary>
  203. /// <remarks>
  204. /// This is also the Zip version for the library when comparing against the version required to extract
  205. /// for an entry. See <see cref="ZipEntry.CanDecompress"/>.
  206. /// </remarks>
  207. public const int VersionMadeBy = 51; // was 45 before AES
  208. /// <summary>
  209. /// The version made by field for entries in the central header when created by this library
  210. /// </summary>
  211. /// <remarks>
  212. /// This is also the Zip version for the library when comparing against the version required to extract
  213. /// for an entry. See <see cref="ZipInputStream.CanDecompressEntry">ZipInputStream.CanDecompressEntry</see>.
  214. /// </remarks>
  215. [Obsolete("Use VersionMadeBy instead")]
  216. public const int VERSION_MADE_BY = 51;
  217. /// <summary>
  218. /// The minimum version required to support strong encryption
  219. /// </summary>
  220. public const int VersionStrongEncryption = 50;
  221. /// <summary>
  222. /// The minimum version required to support strong encryption
  223. /// </summary>
  224. [Obsolete("Use VersionStrongEncryption instead")]
  225. public const int VERSION_STRONG_ENCRYPTION = 50;
  226. /// <summary>
  227. /// Version indicating AES encryption
  228. /// </summary>
  229. public const int VERSION_AES = 51;
  230. /// <summary>
  231. /// The version required for Zip64 extensions (4.5 or higher)
  232. /// </summary>
  233. public const int VersionZip64 = 45;
  234. #endregion Versions
  235. #region Header Sizes
  236. /// <summary>
  237. /// Size of local entry header (excluding variable length fields at end)
  238. /// </summary>
  239. public const int LocalHeaderBaseSize = 30;
  240. /// <summary>
  241. /// Size of local entry header (excluding variable length fields at end)
  242. /// </summary>
  243. [Obsolete("Use LocalHeaderBaseSize instead")]
  244. public const int LOCHDR = 30;
  245. /// <summary>
  246. /// Size of Zip64 data descriptor
  247. /// </summary>
  248. public const int Zip64DataDescriptorSize = 24;
  249. /// <summary>
  250. /// Size of data descriptor
  251. /// </summary>
  252. public const int DataDescriptorSize = 16;
  253. /// <summary>
  254. /// Size of data descriptor
  255. /// </summary>
  256. [Obsolete("Use DataDescriptorSize instead")]
  257. public const int EXTHDR = 16;
  258. /// <summary>
  259. /// Size of central header entry (excluding variable fields)
  260. /// </summary>
  261. public const int CentralHeaderBaseSize = 46;
  262. /// <summary>
  263. /// Size of central header entry
  264. /// </summary>
  265. [Obsolete("Use CentralHeaderBaseSize instead")]
  266. public const int CENHDR = 46;
  267. /// <summary>
  268. /// Size of end of central record (excluding variable fields)
  269. /// </summary>
  270. public const int EndOfCentralRecordBaseSize = 22;
  271. /// <summary>
  272. /// Size of end of central record (excluding variable fields)
  273. /// </summary>
  274. [Obsolete("Use EndOfCentralRecordBaseSize instead")]
  275. public const int ENDHDR = 22;
  276. /// <summary>
  277. /// Size of 'classic' cryptographic header stored before any entry data
  278. /// </summary>
  279. public const int CryptoHeaderSize = 12;
  280. /// <summary>
  281. /// Size of cryptographic header stored before entry data
  282. /// </summary>
  283. [Obsolete("Use CryptoHeaderSize instead")]
  284. public const int CRYPTO_HEADER_SIZE = 12;
  285. /// <summary>
  286. /// The size of the Zip64 central directory locator.
  287. /// </summary>
  288. public const int Zip64EndOfCentralDirectoryLocatorSize = 20;
  289. #endregion Header Sizes
  290. #region Header Signatures
  291. /// <summary>
  292. /// Signature for local entry header
  293. /// </summary>
  294. public const int LocalHeaderSignature = 'P' | ('K' << 8) | (3 << 16) | (4 << 24);
  295. /// <summary>
  296. /// Signature for local entry header
  297. /// </summary>
  298. [Obsolete("Use LocalHeaderSignature instead")]
  299. public const int LOCSIG = 'P' | ('K' << 8) | (3 << 16) | (4 << 24);
  300. /// <summary>
  301. /// Signature for spanning entry
  302. /// </summary>
  303. public const int SpanningSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
  304. /// <summary>
  305. /// Signature for spanning entry
  306. /// </summary>
  307. [Obsolete("Use SpanningSignature instead")]
  308. public const int SPANNINGSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
  309. /// <summary>
  310. /// Signature for temporary spanning entry
  311. /// </summary>
  312. public const int SpanningTempSignature = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24);
  313. /// <summary>
  314. /// Signature for temporary spanning entry
  315. /// </summary>
  316. [Obsolete("Use SpanningTempSignature instead")]
  317. public const int SPANTEMPSIG = 'P' | ('K' << 8) | ('0' << 16) | ('0' << 24);
  318. /// <summary>
  319. /// Signature for data descriptor
  320. /// </summary>
  321. /// <remarks>
  322. /// This is only used where the length, Crc, or compressed size isnt known when the
  323. /// entry is created and the output stream doesnt support seeking.
  324. /// The local entry cannot be 'patched' with the correct values in this case
  325. /// so the values are recorded after the data prefixed by this header, as well as in the central directory.
  326. /// </remarks>
  327. public const int DataDescriptorSignature = 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
  328. /// <summary>
  329. /// Signature for data descriptor
  330. /// </summary>
  331. /// <remarks>
  332. /// This is only used where the length, Crc, or compressed size isnt known when the
  333. /// entry is created and the output stream doesnt support seeking.
  334. /// The local entry cannot be 'patched' with the correct values in this case
  335. /// so the values are recorded after the data prefixed by this header, as well as in the central directory.
  336. /// </remarks>
  337. [Obsolete("Use DataDescriptorSignature instead")]
  338. public const int EXTSIG = 'P' | ('K' << 8) | (7 << 16) | (8 << 24);
  339. /// <summary>
  340. /// Signature for central header
  341. /// </summary>
  342. [Obsolete("Use CentralHeaderSignature instead")]
  343. public const int CENSIG = 'P' | ('K' << 8) | (1 << 16) | (2 << 24);
  344. /// <summary>
  345. /// Signature for central header
  346. /// </summary>
  347. public const int CentralHeaderSignature = 'P' | ('K' << 8) | (1 << 16) | (2 << 24);
  348. /// <summary>
  349. /// Signature for Zip64 central file header
  350. /// </summary>
  351. public const int Zip64CentralFileHeaderSignature = 'P' | ('K' << 8) | (6 << 16) | (6 << 24);
  352. /// <summary>
  353. /// Signature for Zip64 central file header
  354. /// </summary>
  355. [Obsolete("Use Zip64CentralFileHeaderSignature instead")]
  356. public const int CENSIG64 = 'P' | ('K' << 8) | (6 << 16) | (6 << 24);
  357. /// <summary>
  358. /// Signature for Zip64 central directory locator
  359. /// </summary>
  360. public const int Zip64CentralDirLocatorSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24);
  361. /// <summary>
  362. /// Signature for archive extra data signature (were headers are encrypted).
  363. /// </summary>
  364. public const int ArchiveExtraDataSignature = 'P' | ('K' << 8) | (6 << 16) | (7 << 24);
  365. /// <summary>
  366. /// Central header digitial signature
  367. /// </summary>
  368. public const int CentralHeaderDigitalSignature = 'P' | ('K' << 8) | (5 << 16) | (5 << 24);
  369. /// <summary>
  370. /// Central header digitial signature
  371. /// </summary>
  372. [Obsolete("Use CentralHeaderDigitalSignaure instead")]
  373. public const int CENDIGITALSIG = 'P' | ('K' << 8) | (5 << 16) | (5 << 24);
  374. /// <summary>
  375. /// End of central directory record signature
  376. /// </summary>
  377. public const int EndOfCentralDirectorySignature = 'P' | ('K' << 8) | (5 << 16) | (6 << 24);
  378. /// <summary>
  379. /// End of central directory record signature
  380. /// </summary>
  381. [Obsolete("Use EndOfCentralDirectorySignature instead")]
  382. public const int ENDSIG = 'P' | ('K' << 8) | (5 << 16) | (6 << 24);
  383. #endregion Header Signatures
  384. /// <summary>
  385. /// Default encoding used for string conversion. 0 gives the default system OEM code page.
  386. /// Using the default code page isnt the full solution neccessarily
  387. /// there are many variable factors, codepage 850 is often a good choice for
  388. /// European users, however be careful about compatability.
  389. /// </summary>
  390. [Obsolete("Use ZipStrings instead")]
  391. public static int DefaultCodePage
  392. {
  393. get => ZipStrings.CodePage;
  394. set => ZipStrings.CodePage = value;
  395. }
  396. /// <summary> Depracated wrapper for <see cref="ZipStrings.ConvertToString(byte[], int)"/></summary>
  397. [Obsolete("Use ZipStrings.ConvertToString instead")]
  398. public static string ConvertToString(byte[] data, int count)
  399. => ZipStrings.ConvertToString(data, count);
  400. /// <summary> Depracated wrapper for <see cref="ZipStrings.ConvertToString(byte[])"/></summary>
  401. [Obsolete("Use ZipStrings.ConvertToString instead")]
  402. public static string ConvertToString(byte[] data)
  403. => ZipStrings.ConvertToString(data);
  404. /// <summary> Depracated wrapper for <see cref="ZipStrings.ConvertToStringExt(int, byte[], int)"/></summary>
  405. [Obsolete("Use ZipStrings.ConvertToStringExt instead")]
  406. public static string ConvertToStringExt(int flags, byte[] data, int count)
  407. => ZipStrings.ConvertToStringExt(flags, data, count);
  408. /// <summary> Depracated wrapper for <see cref="ZipStrings.ConvertToStringExt(int, byte[])"/></summary>
  409. [Obsolete("Use ZipStrings.ConvertToStringExt instead")]
  410. public static string ConvertToStringExt(int flags, byte[] data)
  411. => ZipStrings.ConvertToStringExt(flags, data);
  412. /// <summary> Depracated wrapper for <see cref="ZipStrings.ConvertToArray(string)"/></summary>
  413. [Obsolete("Use ZipStrings.ConvertToArray instead")]
  414. public static byte[] ConvertToArray(string str)
  415. => ZipStrings.ConvertToArray(str);
  416. /// <summary> Depracated wrapper for <see cref="ZipStrings.ConvertToArray(int, string)"/></summary>
  417. [Obsolete("Use ZipStrings.ConvertToArray instead")]
  418. public static byte[] ConvertToArray(int flags, string str)
  419. => ZipStrings.ConvertToArray(flags, str);
  420. }
  421. }