Unpacker.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. namespace CcCedict;
  3. use \Exception;
  4. /**
  5. * Class for uncompressing a CC-CEDICT file
  6. *
  7. * @package CcCedict
  8. */
  9. class Unpacker
  10. {
  11. /**
  12. * Path and filename of input
  13. *
  14. * @var string
  15. */
  16. private $inputFile;
  17. /**
  18. * Path and filename of output
  19. *
  20. * @var string
  21. */
  22. private $outputFile;
  23. /**
  24. * constructor
  25. */
  26. public function __construct()
  27. {
  28. // set a default for the temp directory
  29. $this->setTempDirectory(sys_get_temp_dir());
  30. }
  31. /**
  32. * sets the path to the input file
  33. *
  34. * @param string $inputFile
  35. *
  36. * @throws Exception
  37. */
  38. public function setInputFile($inputFile)
  39. {
  40. if (!file_exists($inputFile) || !is_file($inputFile) || !is_readable($inputFile)) {
  41. throw new Exception('Cannot use '.$inputFile.' as input file');
  42. }
  43. $this->inputFile = $inputFile;
  44. }
  45. /**
  46. * sets a temp directory that the file gets uncompressed into
  47. *
  48. * @param string $tmp
  49. *
  50. * @throws Exception
  51. */
  52. public function setTempDirectory($tmp)
  53. {
  54. if (!file_exists($tmp) || !is_dir($tmp) || !is_writable($tmp)) {
  55. throw new Exception('Cannot use '.$tmp.' as a temp directory');
  56. }
  57. $this->outputFile = $this->getOutputFile($tmp);
  58. }
  59. /**
  60. * uncompresses the inputFile
  61. *
  62. * @return string Output file path
  63. */
  64. public function unpack(): string
  65. {
  66. if (substr($this->inputFile, -2) == 'gz') {
  67. $fileContents = '';
  68. $fp = gzopen($this->inputFile, 'r');
  69. if (is_resource($fp)) {
  70. while (!empty($block = gzread($fp, 10000))) {
  71. $fileContents .= $block;
  72. }
  73. file_put_contents($this->outputFile, $fileContents);
  74. gzclose($fp);
  75. }
  76. } elseif (substr($this->inputFile, -3) == 'zip') {
  77. $fileContents = '';
  78. $fp = zip_open($this->inputFile);
  79. if (is_resource($fp)) {
  80. $entry = zip_read($fp);
  81. if (is_resource($entry)) {
  82. zip_entry_open($fp, $entry);
  83. while (!empty($block = zip_entry_read($entry))) {
  84. $fileContents .= $block;
  85. }
  86. file_put_contents($this->outputFile, $fileContents);
  87. zip_close($fp);
  88. }
  89. }
  90. } else {
  91. copy($this->inputFile, $this->outputFile);
  92. }
  93. return $this->outputFile;
  94. }
  95. /**
  96. * removes the output file
  97. */
  98. public function removeOutputFile()
  99. {
  100. if (file_exists($this->outputFile) && is_writable($this->outputFile)) {
  101. unlink($this->outputFile);
  102. }
  103. }
  104. /**
  105. * gets the path to the output file
  106. *
  107. * @param string $tmp Temp directory
  108. *
  109. * @return string
  110. */
  111. private function getOutputFile($tmp): string
  112. {
  113. // clean up any existing outputFile
  114. $this->removeOutputFile();
  115. return tempnam($tmp, 'CcCedict');
  116. }
  117. }