ZipCompressionMethod.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace PhpZip\Constants;
  3. use PhpZip\Exception\ZipUnsupportMethodException;
  4. /**
  5. * Class ZipCompressionMethod.
  6. */
  7. final class ZipCompressionMethod
  8. {
  9. /** @var int Compression method Store */
  10. const STORED = 0;
  11. /** @var int Compression method Deflate */
  12. const DEFLATED = 8;
  13. /** @var int Compression method Bzip2 */
  14. const BZIP2 = 12;
  15. /** @var int Compression method AES-Encryption */
  16. const WINZIP_AES = 99;
  17. /** @var array Compression Methods */
  18. private static $ZIP_COMPRESSION_METHODS = [
  19. self::STORED => 'Stored',
  20. 1 => 'Shrunk',
  21. 2 => 'Reduced compression factor 1',
  22. 3 => 'Reduced compression factor 2',
  23. 4 => 'Reduced compression factor 3',
  24. 5 => 'Reduced compression factor 4',
  25. 6 => 'Imploded',
  26. 7 => 'Reserved for Tokenizing compression algorithm',
  27. self::DEFLATED => 'Deflated',
  28. 9 => 'Enhanced Deflating using Deflate64(tm)',
  29. 10 => 'PKWARE Data Compression Library Imploding',
  30. 11 => 'Reserved by PKWARE',
  31. self::BZIP2 => 'BZIP2',
  32. 13 => 'Reserved by PKWARE',
  33. 14 => 'LZMA',
  34. 15 => 'Reserved by PKWARE',
  35. 16 => 'Reserved by PKWARE',
  36. 17 => 'Reserved by PKWARE',
  37. 18 => 'File is compressed using IBM TERSE (new)',
  38. 19 => 'IBM LZ77 z Architecture (PFS)',
  39. 96 => 'WinZip JPEG Compression',
  40. 97 => 'WavPack compressed data',
  41. 98 => 'PPMd version I, Rev 1',
  42. self::WINZIP_AES => 'AES Encryption',
  43. ];
  44. /**
  45. * @param int $value
  46. *
  47. * @return string
  48. */
  49. public static function getCompressionMethodName($value)
  50. {
  51. return isset(self::$ZIP_COMPRESSION_METHODS[$value]) ?
  52. self::$ZIP_COMPRESSION_METHODS[$value] :
  53. 'Unknown Method';
  54. }
  55. /**
  56. * @return int[]
  57. */
  58. public static function getSupportMethods()
  59. {
  60. static $methods;
  61. if ($methods === null) {
  62. $methods = [
  63. self::STORED,
  64. self::DEFLATED,
  65. ];
  66. if (\extension_loaded('bz2')) {
  67. $methods[] = self::BZIP2;
  68. }
  69. }
  70. return $methods;
  71. }
  72. /**
  73. * @param int $compressionMethod
  74. *
  75. * @throws ZipUnsupportMethodException
  76. */
  77. public static function checkSupport($compressionMethod)
  78. {
  79. $compressionMethod = (int) $compressionMethod;
  80. if (!\in_array($compressionMethod, self::getSupportMethods(), true)) {
  81. throw new ZipUnsupportMethodException(sprintf(
  82. 'Compression method %d (%s) is not supported.',
  83. $compressionMethod,
  84. self::getCompressionMethodName($compressionMethod)
  85. ));
  86. }
  87. }
  88. }