| @@ 15-92 (lines=78) @@ | ||
| 12 | use Psr\Log\LoggerAwareInterface; | |
| 13 | use Psr\Log\LogLevel; | |
| 14 | ||
| 15 | class Gzip implements CompressorInterface, DeCompressorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface | |
| 16 | { | |
| 17 | use OptionalLoggerTrait; | |
| 18 | use FileProcessTrait; | |
| 19 | use GetOptionTrait; | |
| 20 | ||
| 21 | /** | |
| 22 | * Compress a file and return the new file | |
| 23 | * | |
| 24 | * @param FileNodeInterface $node | |
| 25 | * @param array $options | |
| 26 | * | |
| 27 | * @return FileNodeInterface | |
| 28 | */ | |
| 29 | public function compress(FileNodeInterface $node, array $options = []) | |
| 30 |     { | |
| 31 |         if (!($node instanceof LocalFile)) { | |
| 32 |             throw new InvalidArgumentException("Node: $node should be a LocalFile"); | |
| 33 | } | |
| 34 | ||
| 35 | $this->options = $options; | |
| 36 | $pathInfo = pathinfo($node->getPath()); | |
| 37 | ||
| 38 |         if (!$node->exists()) { | |
| 39 |             throw new InvalidArgumentException("The file: $node does not exist"); | |
| 40 | } | |
| 41 | ||
| 42 | $outputFile = $node->getClone() | |
| 43 | ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.gz') | |
| 44 | ->setCompression(CompressionType::GZIP); | |
| 45 | ||
| 46 |         $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [ | |
| 47 | 'file' => $node, | |
| 48 | 'target' => $outputFile, | |
| 49 | 'compression' => CompressionType::GZIP, | |
| 50 | ]); | |
| 51 | ||
| 52 |         $cmd = "gzip -c {$node->getPath()} > {$outputFile->getPath()}"; | |
| 53 | ||
| 54 |         return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true)); | |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Decompress a file and return the decompressed file | |
| 59 | * | |
| 60 | * @param FileNodeInterface $node | |
| 61 | * @param array $options | |
| 62 | * | |
| 63 | * @return FileNodeInterface | |
| 64 | */ | |
| 65 | public function decompress(FileNodeInterface $node, array $options = []) | |
| 66 |     { | |
| 67 |         if (!($node instanceof LocalFile)) { | |
| 68 |             throw new InvalidArgumentException("Node: $node should be a LocalFile"); | |
| 69 | } | |
| 70 | ||
| 71 | $this->options = $options; | |
| 72 | $pathInfo = pathinfo($node->getPath()); | |
| 73 | ||
| 74 |         if (!$node->exists()) { | |
| 75 |             throw new InvalidArgumentException("The file: $node does not exist"); | |
| 76 | } | |
| 77 | ||
| 78 | $outputFile = $node->getClone() | |
| 79 | ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename']) | |
| 80 | ->setCompression(CompressionType::NONE); | |
| 81 | ||
| 82 |         $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [ | |
| 83 | 'file' => $node, | |
| 84 | 'target' => $outputFile, | |
| 85 | 'compression' => CompressionType::GZIP, | |
| 86 | ]); | |
| 87 | ||
| 88 |         $cmd = "gunzip -c {$node->getPath()} > {$outputFile->getPath()}"; | |
| 89 | ||
| 90 |         return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true)); | |
| 91 | } | |
| 92 | } | |
| 93 | ||
| @@ 16-92 (lines=77) @@ | ||
| 13 | use Psr\Log\LogLevel; | |
| 14 | use Symfony\Component\Process\Exception\ProcessFailedException; | |
| 15 | ||
| 16 | class Zip implements CompressorInterface, DeCompressorInterface, LoggerAwareInterface, ProcessFactoryAwareInterface | |
| 17 | { | |
| 18 | use OptionalLoggerTrait; | |
| 19 | use FileProcessTrait; | |
| 20 | use GetOptionTrait; | |
| 21 | ||
| 22 | /** | |
| 23 | * Compress a file and return the new file | |
| 24 | * | |
| 25 | * @param FileNodeInterface $node | |
| 26 | * @param array $options | |
| 27 | * | |
| 28 | * @return FileNodeInterface | |
| 29 | */ | |
| 30 | public function compress(FileNodeInterface $node, array $options = []) | |
| 31 |     { | |
| 32 |         if (!($node instanceof LocalFile)) { | |
| 33 |             throw new InvalidArgumentException("Node: $node should be a LocalFile"); | |
| 34 | } | |
| 35 | ||
| 36 | $this->options = $options; | |
| 37 | $pathInfo = pathinfo($node->getPath()); | |
| 38 | ||
| 39 |         if (!$node->exists()) { | |
| 40 |             throw new InvalidArgumentException("The file: $node does not exist"); | |
| 41 | } | |
| 42 | ||
| 43 | $outputFile = $node->getClone() | |
| 44 | ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename'] . '.zip') | |
| 45 | ->setCompression(CompressionType::ZIP); | |
| 46 | ||
| 47 |         $this->log(LogLevel::INFO, "Compressing file: {file} into {target} using {compression}", [ | |
| 48 | 'file' => $node, | |
| 49 | 'target' => $outputFile, | |
| 50 | 'compression' => CompressionType::ZIP, | |
| 51 | ]); | |
| 52 |         $cmd = "zip {$outputFile->getPath()} {$node->getPath()}"; | |
| 53 | ||
| 54 |         return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true)); | |
| 55 | } | |
| 56 | ||
| 57 | /** | |
| 58 | * Decompress a file and return the decompressed file | |
| 59 | * | |
| 60 | * @param FileNodeInterface $node | |
| 61 | * @param array $options | |
| 62 | * | |
| 63 | * @return FileNodeInterface | |
| 64 | */ | |
| 65 | public function decompress(FileNodeInterface $node, array $options = []) | |
| 66 |     { | |
| 67 |         if (!($node instanceof LocalFile)) { | |
| 68 |             throw new InvalidArgumentException("Node: $node should be a LocalFile"); | |
| 69 | } | |
| 70 | ||
| 71 | $this->options = $options; | |
| 72 | $pathInfo = pathinfo($node->getPath()); | |
| 73 | ||
| 74 |         if (!$node->exists()) { | |
| 75 |             throw new InvalidArgumentException("The file: $node does not exist"); | |
| 76 | } | |
| 77 | ||
| 78 | $outputFile = $node->getClone() | |
| 79 | ->setPath($pathInfo['dirname'] . '/' . $pathInfo['filename']) | |
| 80 | ->setCompression(CompressionType::NONE); | |
| 81 | ||
| 82 |         $this->log(LogLevel::INFO, "DeCompressing file: {file} into {target} using {compression}", [ | |
| 83 | 'file' => $node, | |
| 84 | 'target' => $outputFile, | |
| 85 | 'compression' => CompressionType::ZIP, | |
| 86 | ]); | |
| 87 | ||
| 88 |         $cmd = "unzip -p {$node->getPath()} > {$outputFile->getPath()}"; | |
| 89 | ||
| 90 |         return $this->processFile($node, $outputFile, $cmd, $this->getOption('keepOldFile', true)); | |
| 91 | } | |
| 92 | } | |
| 93 | ||