-
Notifications
You must be signed in to change notification settings - Fork 9.4k
/
Copy pathArchive.php
201 lines (188 loc) · 5.4 KB
/
Archive.php
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Framework;
use Magento\Framework\Archive\Bz;
use Magento\Framework\Archive\Gz;
use Magento\Framework\Archive\Tar;
/**
* Class to work with archives
*
* @author Magento Core Team <[email protected]>
*/
class Archive
{
/**
* Archiver is used for compress.
*/
public const DEFAULT_ARCHIVER = 'gz';
/**
* Default packer for directory.
*/
public const TAPE_ARCHIVER = 'tar';
/**
* Current archiver is used for compress.
*
* @var \Magento\Framework\Archive\Tar|\Magento\Framework\Archive\Gz|\Magento\Framework\Archive\Bz
*/
protected $_archiver = null;
/**
* Accessible formats for compress.
*
* @var array
*/
protected $_formats = [
'tar' => 'tar',
'gz' => 'gz',
'gzip' => 'gz',
'tgz' => 'tar.gz',
'tgzip' => 'tar.gz',
'bz' => 'bz',
'bzip' => 'bz',
'bzip2' => 'bz',
'bz2' => 'bz',
'tbz' => 'tar.bz',
'tbzip' => 'tar.bz',
'tbz2' => 'tar.bz',
'tbzip2' => 'tar.bz',
];
/**
* Create object of current archiver by $extension.
*
* @param string $extension
* @return Tar|Gz|Bz
*/
protected function _getArchiver($extension)
{
$extension = $extension !== null ? strtolower($extension) : '';
$format = $this->_formats[$extension] ?? self::DEFAULT_ARCHIVER;
$class = '\\Magento\Framework\Archive\\' . ucfirst($format);
$this->_archiver = new $class();
return $this->_archiver;
}
/**
* Split current format to list of archivers.
*
* @param string $source
* @return string[]|string
*/
protected function _getArchivers($source)
{
$ext = pathinfo($source, PATHINFO_EXTENSION);
if (!empty($this->_formats[$ext])) {
return explode('.', $this->_formats[$ext]);
}
return [];
}
/**
* Pack file or directory to archivers are parsed from extension.
*
* @param string $source
* @param string $destination
* @param boolean $skipRoot skip first level parent
* @return string Path to file
*/
public function pack($source, $destination = 'packed.tgz', $skipRoot = false)
{
$archivers = $this->_getArchivers($destination);
$interimSource = '';
for ($i = 0, $count = count($archivers); $i < $count; $i++) {
if ($i == $count - 1) {
$packed = $destination;
} else {
$packed = dirname($destination) . '/~tmp-' . microtime(true) . $archivers[$i] . '.' . $archivers[$i];
}
$source = $this->_getArchiver($archivers[$i])->pack($source, $packed, $skipRoot);
if ($interimSource && $i < $count) {
unlink($interimSource);
}
$interimSource = $source;
}
return $source;
}
/**
* Unpack file from archivers are parsed from extension.
*
* If $tillTar == true unpack file from archivers till
* meet TAR archiver.
*
* @param string $source
* @param string $destination
* @param bool $tillTar
* @param bool $clearInterm
* @return string Path to file
*/
public function unpack($source, $destination = '.', $tillTar = false, $clearInterm = true)
{
$archivers = $this->_getArchivers($source);
$interimSource = '';
for ($i = count($archivers) - 1; $i >= 0; $i--) {
if ($tillTar && $archivers[$i] == self::TAPE_ARCHIVER) {
break;
}
if ($i == 0) {
$packed = rtrim($destination, '/') . '/';
} else {
$packed = rtrim(
$destination,
'/'
) . '/~tmp-' . microtime(
true
) . $archivers[$i - 1] . '.' . $archivers[$i - 1];
}
$source = $this->_getArchiver($archivers[$i])->unpack($source, $packed);
if ($clearInterm && $interimSource && $i >= 0) {
unlink($interimSource);
}
$interimSource = $source;
}
return $source;
}
/**
* Extract one file from TAR (Tape Archiver).
*
* @param string $file
* @param string $source
* @param string $destination
* @return string Path to file
*/
public function extract($file, $source, $destination = '.')
{
$tarFile = $this->unpack($source, $destination, true);
$resFile = $this->_getArchiver(self::TAPE_ARCHIVER)->extract($file, $tarFile, $destination);
if (!$this->isTar($source)) {
unlink($tarFile);
}
return $resFile;
}
/**
* Check file is archive.
*
* @param string $file
* @return boolean
*/
public function isArchive($file)
{
$archivers = $this->_getArchivers($file);
if (count($archivers)) {
return true;
}
return false;
}
/**
* Check file is TAR.
*
* @param string $file
* @return boolean
*/
public function isTar($file)
{
$archivers = $this->_getArchivers($file);
if (count($archivers) == 1 && $archivers[0] == self::TAPE_ARCHIVER) {
return true;
}
return false;
}
}