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