PageRenderTime 54ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/Common/SharpZipLib/Zip/ZipConstants.cs

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