-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathRecursiveCompressor.php
More file actions
152 lines (129 loc) · 4.61 KB
/
Copy pathRecursiveCompressor.php
File metadata and controls
152 lines (129 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
* _ _ _ _
* | (_) | (_)
* | |_| |__ __ _ ___ _ _ _ __ ___ _ ___
* | | | '_ \ / _` / __| | | | '_ \ / __| |/ _ \
* | | | |_) | (_| \__ \ |_| | | | | (__| | (_) |
* |_|_|_.__/ \__,_|___/\__, |_| |_|\___|_|\___/
* __/ |
* |___/
*
* Copyright (C) 2016-2026 NetherGames Network
*
* This is private software, you cannot redistribute and/or modify it in any way
* unless given explicit permission to do so. If you have not been given explicit
* permission to view or modify this software you should take the appropriate actions
* to remove this software from your device immediately.
*
* @author driesboy
*
*/
declare(strict_types=1);
namespace libasyncio;
use GlobalLogger;
use libasyncio\compression\CompressionFormat;
use libasyncio\compression\Compressor;
use Phar;
use PharData;
use pocketmine\utils\Filesystem;
use RuntimeException;
use Throwable;
use function is_dir;
use function is_file;
use function mkdir;
use function str_ends_with;
class RecursiveCompressor
{
public const ARCHIVE_FORMAT = 'tar';
/**
* Compress a directory.
* The output should be a directory
* like path. It's important you don't
* use a file name for it.
*
* Output format is the chosen compression format.
*
* @param string $input
* @param string $output
* @param int|null $compressionLevel
* @param CompressionFormat|null $format
*
* @return bool
*/
public static function compress(string $input, string $output, ?int $compressionLevel = null, ?CompressionFormat $format = null): bool
{
$compressor = self::resolveCompressor($format);
$archive = new PharData($input . '.' . self::ARCHIVE_FORMAT);
$archive->buildFromDirectory($input);
$data = file_get_contents($archive->getPath());
if ($data === false) {
throw new RuntimeException('Archive unreadable');
}
$compressedData = $compressor->compress($data, $compressionLevel);
Filesystem::safeFilePutContents($output . '.' . $compressor->getFormat()->getFileExtension(), $compressedData);
unset($archive);
Phar::unlinkArchive($input . '.' . self::ARCHIVE_FORMAT);
return true;
}
/**
* Decompress a directory.
* The input should be a directory
* like path. It's important you don't
* use a file name for it.
*
* Input format is the chosen compression format.
* Output format is regular directory.
*
* @param string $input
* @param string $output
* @param CompressionFormat|null $format
*
* @return bool
*/
public static function uncompress(string $input, string $output, ?CompressionFormat $format = null): bool
{
$compressor = self::resolveCompressor($format, $input);
$extension = $compressor->getFormat()->getFileExtension();
if (!str_ends_with($input, '.' . $extension)) {
$input .= '.' . $extension;
}
if (!is_file($input)) {
throw new RuntimeException(
'That file is not of type ' . $extension . ', cannot uncompress'
);
}
$compressedData = file_get_contents($input);
if ($compressedData === false) {
throw new RuntimeException('Compressed file unreadable');
}
$data = $compressor->decompress($compressedData);
Filesystem::safeFilePutContents($output . '.' . self::ARCHIVE_FORMAT, $data);
$archive = new PharData($output . '.' . self::ARCHIVE_FORMAT);
try {
if (!is_dir($output) && !mkdir($output)) {
throw new RuntimeException('Directory "' . $output . '" was not created');
}
} catch (Throwable $exception) {
GlobalLogger::get()->critical("Unhandled exception from a method that should never throw anything.");
GlobalLogger::get()->logException($exception);
}
$archive->extractTo($output);
unset($archive);
PharData::unlinkArchive($output . '.' . self::ARCHIVE_FORMAT);
return true;
}
/**
* @param CompressionFormat|null $format
* @param string|null $path
*
* @return Compressor
*/
private static function resolveCompressor(?CompressionFormat $format, ?string $path = null): Compressor
{
if ($format !== null) {
return $format->getCompressor();
}
return ($path !== null ? (CompressionFormat::fromPath($path) ?? CompressionFormat::auto()) : CompressionFormat::auto())->getCompressor();
}
}