ZipConstants.cs 17 KB

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