123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131 |
- <?php
- namespace CcCedict;
- use \Exception;
- /**
- * Class for uncompressing a CC-CEDICT file
- *
- * @package CcCedict
- */
- class Unpacker
- {
- /**
- * Path and filename of input
- *
- * @var string
- */
- private $inputFile;
- /**
- * Path and filename of output
- *
- * @var string
- */
- private $outputFile;
- /**
- * constructor
- */
- public function __construct()
- {
- // set a default for the temp directory
- $this->setTempDirectory(sys_get_temp_dir());
- }
- /**
- * sets the path to the input file
- *
- * @param string $inputFile
- *
- * @throws Exception
- */
- public function setInputFile($inputFile)
- {
- if (!file_exists($inputFile) || !is_file($inputFile) || !is_readable($inputFile)) {
- throw new Exception('Cannot use '.$inputFile.' as input file');
- }
- $this->inputFile = $inputFile;
- }
- /**
- * sets a temp directory that the file gets uncompressed into
- *
- * @param string $tmp
- *
- * @throws Exception
- */
- public function setTempDirectory($tmp)
- {
- if (!file_exists($tmp) || !is_dir($tmp) || !is_writable($tmp)) {
- throw new Exception('Cannot use '.$tmp.' as a temp directory');
- }
- $this->outputFile = $this->getOutputFile($tmp);
- }
- /**
- * uncompresses the inputFile
- *
- * @return string Output file path
- */
- public function unpack(): string
- {
- if (substr($this->inputFile, -2) == 'gz') {
- $fileContents = '';
- $fp = gzopen($this->inputFile, 'r');
- if (is_resource($fp)) {
- while (!empty($block = gzread($fp, 10000))) {
- $fileContents .= $block;
- }
- file_put_contents($this->outputFile, $fileContents);
- gzclose($fp);
- }
- } elseif (substr($this->inputFile, -3) == 'zip') {
- $fileContents = '';
- $fp = zip_open($this->inputFile);
- if (is_resource($fp)) {
- $entry = zip_read($fp);
- if (is_resource($entry)) {
- zip_entry_open($fp, $entry);
- while (!empty($block = zip_entry_read($entry))) {
- $fileContents .= $block;
- }
- file_put_contents($this->outputFile, $fileContents);
- zip_close($fp);
- }
- }
- } else {
- copy($this->inputFile, $this->outputFile);
- }
- return $this->outputFile;
- }
- /**
- * removes the output file
- */
- public function removeOutputFile()
- {
- if (file_exists($this->outputFile) && is_writable($this->outputFile)) {
- unlink($this->outputFile);
- }
- }
- /**
- * gets the path to the output file
- *
- * @param string $tmp Temp directory
- *
- * @return string
- */
- private function getOutputFile($tmp): string
- {
- // clean up any existing outputFile
- $this->removeOutputFile();
- return tempnam($tmp, 'CcCedict');
- }
- }
|