Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Archive.php
Go to the documentation of this file.
1 <?php
6 namespace Magento\Framework;
7 
11 
17 class Archive
18 {
22  const DEFAULT_ARCHIVER = 'gz';
23 
27  const TAPE_ARCHIVER = 'tar';
28 
34  protected $_archiver = null;
35 
41  protected $_formats = [
42  'tar' => 'tar',
43  'gz' => 'gz',
44  'gzip' => 'gz',
45  'tgz' => 'tar.gz',
46  'tgzip' => 'tar.gz',
47  'bz' => 'bz',
48  'bzip' => 'bz',
49  'bzip2' => 'bz',
50  'bz2' => 'bz',
51  'tbz' => 'tar.bz',
52  'tbzip' => 'tar.bz',
53  'tbz2' => 'tar.bz',
54  'tbzip2' => 'tar.bz',
55  ];
56 
63  protected function _getArchiver($extension)
64  {
65  $extension = strtolower($extension);
66  $format = isset($this->_formats[$extension]) ? $this->_formats[$extension] : self::DEFAULT_ARCHIVER;
67  $class = '\\Magento\Framework\Archive\\' . ucfirst($format);
68  $this->_archiver = new $class();
69  return $this->_archiver;
70  }
71 
78  protected function _getArchivers($source)
79  {
80  $ext = pathinfo($source, PATHINFO_EXTENSION);
81  if (!empty($this->_formats[$ext])) {
82  return explode('.', $this->_formats[$ext]);
83  }
84  return [];
85  }
86 
95  public function pack($source, $destination = 'packed.tgz', $skipRoot = false)
96  {
97  $archivers = $this->_getArchivers($destination);
98  $interimSource = '';
99  for ($i = 0; $i < count($archivers); $i++) {
100  if ($i == count($archivers) - 1) {
101  $packed = $destination;
102  } else {
103  $packed = dirname($destination) . '/~tmp-' . microtime(true) . $archivers[$i] . '.' . $archivers[$i];
104  }
105  $source = $this->_getArchiver($archivers[$i])->pack($source, $packed, $skipRoot);
106  if ($interimSource && $i < count($archivers)) {
107  unlink($interimSource);
108  }
109  $interimSource = $source;
110  }
111  return $source;
112  }
113 
125  public function unpack($source, $destination = '.', $tillTar = false, $clearInterm = true)
126  {
127  $archivers = $this->_getArchivers($source);
128  $interimSource = '';
129  for ($i = count($archivers) - 1; $i >= 0; $i--) {
130  if ($tillTar && $archivers[$i] == self::TAPE_ARCHIVER) {
131  break;
132  }
133  if ($i == 0) {
134  $packed = rtrim($destination, '/') . '/';
135  } else {
136  $packed = rtrim(
137  $destination,
138  '/'
139  ) . '/~tmp-' . microtime(
140  true
141  ) . $archivers[$i - 1] . '.' . $archivers[$i - 1];
142  }
143  $source = $this->_getArchiver($archivers[$i])->unpack($source, $packed);
144 
145  if ($clearInterm && $interimSource && $i >= 0) {
146  unlink($interimSource);
147  }
148  $interimSource = $source;
149  }
150  return $source;
151  }
152 
161  public function extract($file, $source, $destination = '.')
162  {
163  $tarFile = $this->unpack($source, $destination, true);
164  $resFile = $this->_getArchiver(self::TAPE_ARCHIVER)->extract($file, $tarFile, $destination);
165  if (!$this->isTar($source)) {
166  unlink($tarFile);
167  }
168  return $resFile;
169  }
170 
177  public function isArchive($file)
178  {
179  $archivers = $this->_getArchivers($file);
180  if (count($archivers)) {
181  return true;
182  }
183  return false;
184  }
185 
192  public function isTar($file)
193  {
194  $archivers = $this->_getArchivers($file);
195  if (count($archivers) == 1 && $archivers[0] == self::TAPE_ARCHIVER) {
196  return true;
197  }
198  return false;
199  }
200 }
_getArchiver($extension)
Definition: Archive.php:63
unpack($source, $destination='.', $tillTar=false, $clearInterm=true)
Definition: Archive.php:125
$source
Definition: source.php:23
pack($source, $destination='packed.tgz', $skipRoot=false)
Definition: Archive.php:95
$_option $_optionId $class
Definition: date.phtml:13
$format
Definition: list.phtml:12
extract($file, $source, $destination='.')
Definition: Archive.php:161
$i
Definition: gallery.phtml:31