1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
/** |
5
|
|
|
* @copyright Copyright (c) 2019-2023 Matias De lellis <[email protected]> |
6
|
|
|
* |
7
|
|
|
* @author Matias De lellis <[email protected]> |
8
|
|
|
* |
9
|
|
|
* @license GNU AGPL version 3 or any later version |
10
|
|
|
* |
11
|
|
|
* This program is free software: you can redistribute it and/or modify |
12
|
|
|
* it under the terms of the GNU Affero General Public License as |
13
|
|
|
* published by the Free Software Foundation, either version 3 of the |
14
|
|
|
* License, or (at your option) any later version. |
15
|
|
|
* |
16
|
|
|
* This program is distributed in the hope that it will be useful, |
17
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
18
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
19
|
|
|
* GNU Affero General Public License for more details. |
20
|
|
|
* |
21
|
|
|
* You should have received a copy of the GNU Affero General Public License |
22
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
23
|
|
|
* |
24
|
|
|
*/ |
25
|
|
|
|
26
|
|
|
namespace OCA\FaceRecognition\Service; |
27
|
|
|
|
28
|
|
|
class CompressionService { |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Uncompressing the file with the bzip2-extension |
32
|
|
|
* |
33
|
|
|
* @param string $inputFile |
34
|
|
|
* @param string $outputFile |
35
|
|
|
* |
36
|
|
|
* @throws \Exception |
37
|
|
|
* |
38
|
|
|
* @return void |
39
|
|
|
*/ |
40
|
1 |
|
public function bunzip2(string $inputFile, string $outputFile): void { |
41
|
1 |
|
if (!file_exists ($inputFile) || !is_readable ($inputFile)) |
42
|
|
|
throw new \Exception('The file ' . $inputFile . ' not exists or is not readable'); |
43
|
|
|
|
44
|
1 |
|
if ((!file_exists($outputFile) && !is_writeable(dirname($outputFile))) || |
45
|
1 |
|
(file_exists($outputFile) && !is_writable($outputFile))) |
46
|
|
|
throw new \Exception('The file ' . $outputFile . ' exists or is not writable'); |
47
|
|
|
|
48
|
1 |
|
$in_file = bzopen ($inputFile, "r"); |
49
|
1 |
|
$out_file = fopen ($outputFile, "w"); |
50
|
|
|
|
51
|
1 |
|
if ($out_file === false) |
52
|
|
|
throw new \Exception('Could not open the file to write: ' . $outputFile); |
53
|
|
|
|
54
|
1 |
|
while ($buffer = bzread ($in_file, 4096)) { |
55
|
1 |
|
if($buffer === false) |
56
|
|
|
throw new \Exception('Read problem: ' . bzerrstr($in_file)); |
57
|
1 |
|
if(bzerrno($in_file) !== 0) |
58
|
|
|
throw new \Exception('Compression problem: '. bzerrstr($in_file)); |
59
|
|
|
|
60
|
1 |
|
fwrite ($out_file, $buffer, 4096); |
61
|
|
|
} |
62
|
|
|
|
63
|
1 |
|
bzclose ($in_file); |
64
|
1 |
|
fclose ($out_file); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
} |
68
|
|
|
|