|
1
|
|
|
<?php |
|
2
|
|
|
namespace wapmorgan\UnifiedArchive\Formats; |
|
3
|
|
|
|
|
4
|
|
|
use Exception; |
|
5
|
|
|
use wapmorgan\UnifiedArchive\ArchiveEntry; |
|
6
|
|
|
use wapmorgan\UnifiedArchive\ArchiveInformation; |
|
7
|
|
|
use wapmorgan\UnifiedArchive\UnsupportedOperationException; |
|
8
|
|
|
|
|
9
|
|
|
abstract class OneFileFormat extends BasicFormat |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var null|string Should be filled for real format like 'gz' or other */ |
|
12
|
|
|
const FORMAT_SUFFIX = null; |
|
13
|
|
|
|
|
14
|
|
|
protected $fileName; |
|
15
|
|
|
protected $inArchiveFileName; |
|
16
|
|
|
protected $uncompressedSize; |
|
17
|
|
|
protected $modificationTime; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* BasicFormat constructor. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $archiveFileName |
|
23
|
|
|
* |
|
24
|
|
|
* @throws \Exception |
|
25
|
|
|
*/ |
|
26
|
|
|
public function __construct($archiveFileName) |
|
27
|
|
|
{ |
|
28
|
|
|
if (static::FORMAT_SUFFIX === null) |
|
|
|
|
|
|
29
|
|
|
throw new \Exception('Format should be initialized'); |
|
30
|
|
|
$this->fileName = $archiveFileName; |
|
31
|
|
|
$this->inArchiveFileName = basename($archiveFileName, '.'.self::FORMAT_SUFFIX); |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* @return ArchiveInformation |
|
36
|
|
|
*/ |
|
37
|
|
|
public function getArchiveInformation() |
|
38
|
|
|
{ |
|
39
|
|
|
$information = new ArchiveInformation(); |
|
40
|
|
|
$information->compressedFilesSize = filesize($this->fileName); |
|
41
|
|
|
$information->uncompressedFilesSize = $this->uncompressedSize; |
|
42
|
|
|
$information->files[] = $this->inArchiveFileName; |
|
43
|
|
|
return $information; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return array |
|
48
|
|
|
*/ |
|
49
|
|
|
public function getFileNames() |
|
50
|
|
|
{ |
|
51
|
|
|
return [$this->inArchiveFileName]; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $fileName |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool |
|
58
|
|
|
*/ |
|
59
|
|
|
public function isFileExists($fileName) |
|
60
|
|
|
{ |
|
61
|
|
|
return $fileName === $this->inArchiveFileName; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @param string $fileName |
|
66
|
|
|
* |
|
67
|
|
|
* @return ArchiveEntry|false |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getFileData($fileName) |
|
70
|
|
|
{ |
|
71
|
|
|
return new ArchiveEntry($this->inArchiveFileName, filesize($this->fileName), |
|
72
|
|
|
$this->uncompressedSize, $this->modificationTime); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* @param string $outputFolder |
|
77
|
|
|
* @param array $files |
|
78
|
|
|
* |
|
79
|
|
|
* @return false|int |
|
80
|
|
|
* @throws \Exception |
|
81
|
|
|
*/ |
|
82
|
|
|
public function extractFiles($outputFolder, array $files = null) |
|
83
|
|
|
{ |
|
84
|
|
|
return $this->extractArchive($outputFolder); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @param string $outputFolder |
|
89
|
|
|
* |
|
90
|
|
|
* @return false|int |
|
91
|
|
|
* @throws \Exception |
|
92
|
|
|
*/ |
|
93
|
|
|
public function extractArchive($outputFolder) |
|
94
|
|
|
{ |
|
95
|
|
|
$data = $this->getFileContent($this->inArchiveFileName); |
|
96
|
|
|
if ($data === false) |
|
97
|
|
|
throw new Exception('Could not extract archive'); |
|
98
|
|
|
|
|
99
|
|
|
if (file_put_contents($outputFolder.$this->inArchiveFileName, $data) !== false) |
|
100
|
|
|
return 1; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* @param array $files |
|
105
|
|
|
* |
|
106
|
|
|
* @return false|int |
|
107
|
|
|
* @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException |
|
108
|
|
|
*/ |
|
109
|
|
|
public function deleteFiles(array $files) |
|
110
|
|
|
{ |
|
111
|
|
|
throw new UnsupportedOperationException(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param array $files |
|
116
|
|
|
* |
|
117
|
|
|
* @return false|int |
|
118
|
|
|
* @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException |
|
119
|
|
|
*/ |
|
120
|
|
|
public function addFiles(array $files) |
|
121
|
|
|
{ |
|
122
|
|
|
throw new UnsupportedOperationException(); |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @param array $files |
|
127
|
|
|
* @param string $archiveFileName |
|
128
|
|
|
* |
|
129
|
|
|
* @return false|int |
|
130
|
|
|
* @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException |
|
131
|
|
|
*/ |
|
132
|
|
|
public static function createArchive(array $files, $archiveFileName){ |
|
133
|
|
|
if (count($files) > 1) return false; |
|
134
|
|
|
$filename = array_shift($files); |
|
135
|
|
|
if (is_null($filename)) return false; // invalid list |
|
136
|
|
|
if (file_put_contents($archiveFileName, |
|
137
|
|
|
static::compressData(file_get_contents($filename))) !== false) |
|
138
|
|
|
return 1; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @param $data |
|
143
|
|
|
* |
|
144
|
|
|
* @return mixed |
|
145
|
|
|
* @throws \wapmorgan\UnifiedArchive\UnsupportedOperationException |
|
146
|
|
|
*/ |
|
147
|
|
|
protected static function compressData($data) { |
|
|
|
|
|
|
148
|
|
|
throw new UnsupportedOperationException(); |
|
149
|
|
|
} |
|
150
|
|
|
} |