1
|
|
|
<?php |
2
|
|
|
namespace wapmorgan\UnifiedArchive\Drivers\OneFile; |
3
|
|
|
|
4
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
5
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
6
|
|
|
use wapmorgan\UnifiedArchive\Drivers\BasicDriver; |
7
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveCreationException; |
8
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\ArchiveExtractionException; |
9
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\EmptyFileListException; |
10
|
|
|
use wapmorgan\UnifiedArchive\Exceptions\UnsupportedOperationException; |
11
|
|
|
use wapmorgan\UnifiedArchive\Formats; |
12
|
|
|
|
13
|
|
|
abstract class OneFileDriver extends BasicDriver |
14
|
|
|
{ |
15
|
|
|
/** @var null|string Should be filled for real format like 'gz' or other */ |
16
|
|
|
const FORMAT_SUFFIX = null; |
17
|
|
|
const FORMAT = null; |
18
|
|
|
const PHP_EXTENSION = null; |
19
|
|
|
|
20
|
|
|
const TYPE = self::TYPE_EXTENSION; |
21
|
|
|
|
22
|
|
|
protected $fileName; |
23
|
|
|
protected $inArchiveFileName; |
24
|
|
|
protected $uncompressedSize; |
25
|
|
|
protected $modificationTime; |
26
|
|
|
|
27
|
|
|
public static function isInstalled() |
28
|
|
|
{ |
29
|
|
|
return extension_loaded(static::PHP_EXTENSION); |
|
|
|
|
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
public static function getInstallationInstruction() |
33
|
|
|
{ |
34
|
|
|
return 'install `' . static::PHP_EXTENSION . '` extension'; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public static function getSupportedFormats() |
38
|
|
|
{ |
39
|
|
|
return [static::FORMAT]; |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @param $format |
44
|
|
|
* @return array |
45
|
|
|
*/ |
46
|
|
|
public static function checkFormatSupport($format) |
47
|
|
|
{ |
48
|
|
|
if (!static::isInstalled()) { |
49
|
|
|
return []; |
50
|
|
|
} |
51
|
|
|
switch ($format) { |
52
|
|
|
case static::FORMAT: |
53
|
|
|
return [BasicDriver::OPEN, BasicDriver::EXTRACT_CONTENT, BasicDriver::STREAM_CONTENT, BasicDriver::CREATE]; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @inheritDoc |
59
|
|
|
*/ |
60
|
|
|
public function __construct($archiveFileName, $format, $password = null) |
61
|
|
|
{ |
62
|
|
|
if (static::FORMAT_SUFFIX === null) |
|
|
|
|
63
|
|
|
throw new \Exception('Format should be initialized'); |
64
|
|
|
if ($password !== null) |
65
|
|
|
throw new UnsupportedOperationException(self::FORMAT_SUFFIX.' archive does not support password!'); |
66
|
|
|
|
67
|
|
|
parent::__construct($archiveFileName, $format); |
68
|
|
|
|
69
|
|
|
$this->inArchiveFileName = basename($archiveFileName, '.'.static::FORMAT_SUFFIX); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return ArchiveInformation |
74
|
|
|
*/ |
75
|
|
|
public function getArchiveInformation() |
76
|
|
|
{ |
77
|
|
|
$information = new ArchiveInformation(); |
78
|
|
|
$information->compressedFilesSize = filesize($this->fileName); |
79
|
|
|
$information->uncompressedFilesSize = $this->uncompressedSize; |
80
|
|
|
$information->files[] = $this->inArchiveFileName; |
81
|
|
|
return $information; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return array |
86
|
|
|
*/ |
87
|
|
|
public function getFileNames() |
88
|
|
|
{ |
89
|
|
|
return [$this->inArchiveFileName]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $fileName |
94
|
|
|
* |
95
|
|
|
* @return bool |
96
|
|
|
*/ |
97
|
|
|
public function isFileExists($fileName) |
98
|
|
|
{ |
99
|
|
|
return $fileName === $this->inArchiveFileName; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param string $fileName |
104
|
|
|
* @return ArchiveEntry|false |
105
|
|
|
*/ |
106
|
|
|
public function getFileData($fileName) |
107
|
|
|
{ |
108
|
|
|
return new ArchiveEntry( |
109
|
|
|
$this->inArchiveFileName, |
110
|
|
|
filesize($this->fileName), |
111
|
|
|
$this->uncompressedSize, |
112
|
|
|
$this->modificationTime); |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* @param string $outputFolder |
117
|
|
|
* @param array $files |
118
|
|
|
* @return int |
119
|
|
|
* @throws ArchiveExtractionException |
120
|
|
|
*/ |
121
|
|
|
public function extractFiles($outputFolder, array $files = null) |
122
|
|
|
{ |
123
|
|
|
return $this->extractArchive($outputFolder); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $outputFolder |
128
|
|
|
* @return int |
129
|
|
|
* @throws ArchiveExtractionException |
130
|
|
|
*/ |
131
|
|
|
public function extractArchive($outputFolder) |
132
|
|
|
{ |
133
|
|
|
$data = $this->getFileContent($this->inArchiveFileName); |
134
|
|
|
if ($data === false) |
135
|
|
|
throw new ArchiveExtractionException('Could not extract archive'); |
136
|
|
|
|
137
|
|
|
$size = strlen($data); |
138
|
|
|
$written = file_put_contents($outputFolder.$this->inArchiveFileName, $data); |
139
|
|
|
|
140
|
|
|
if ($written === true) { |
141
|
|
|
throw new ArchiveExtractionException('Could not extract file "'.$this->inArchiveFileName.'": could not write data'); |
142
|
|
|
} else if ($written < $size) { |
143
|
|
|
throw new ArchiveExtractionException('Could not archive file "'.$this->inArchiveFileName.'": written '.$written.' of '.$size); |
144
|
|
|
} |
145
|
|
|
return 1; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @param array $files |
150
|
|
|
* @param string $archiveFileName |
151
|
|
|
* @param int $archiveFormat |
152
|
|
|
* @param int $compressionLevel |
153
|
|
|
* @param null $password |
|
|
|
|
154
|
|
|
* @param $fileProgressCallable |
155
|
|
|
* @return int |
156
|
|
|
* @throws ArchiveCreationException |
157
|
|
|
* @throws UnsupportedOperationException |
158
|
|
|
*/ |
159
|
|
|
public static function createArchive( |
160
|
|
|
array $files, |
161
|
|
|
$archiveFileName, |
162
|
|
|
$archiveFormat, |
163
|
|
|
$compressionLevel = self::COMPRESSION_AVERAGE, |
164
|
|
|
$password = null, |
165
|
|
|
$fileProgressCallable = null |
166
|
|
|
) { |
167
|
|
|
if (count($files) > 1) { |
168
|
|
|
throw new UnsupportedOperationException('One-file format ('.__CLASS__.') could not archive few files'); |
169
|
|
|
} |
170
|
|
|
if (empty($files)) { |
171
|
|
|
throw new EmptyFileListException(); |
172
|
|
|
} |
173
|
|
|
if ($password !== null) { |
|
|
|
|
174
|
|
|
throw new UnsupportedOperationException('One-file format ('.__CLASS__.') could not encrypt an archive'); |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
$filename = array_shift($files); |
178
|
|
|
|
179
|
|
|
$compressed_content = static::compressData(file_get_contents($filename), $compressionLevel); |
180
|
|
|
$size = strlen($compressed_content); |
181
|
|
|
$written = file_put_contents($archiveFileName, $compressed_content); |
182
|
|
|
|
183
|
|
|
if ($written === true) { |
184
|
|
|
throw new ArchiveCreationException('Could not archive file: could not write data'); |
185
|
|
|
} else if ($written < $size) { |
186
|
|
|
throw new ArchiveCreationException('Could not archive file: written '.$written.' of '.$size); |
187
|
|
|
} |
188
|
|
|
return 1; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* @param string $data |
193
|
|
|
* @param int $compressionLevel |
194
|
|
|
* @return mixed |
195
|
|
|
* @throws UnsupportedOperationException |
196
|
|
|
*/ |
197
|
|
|
abstract protected static function compressData($data, $compressionLevel); |
198
|
|
|
} |
199
|
|
|
|