Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
archiver.php
1<?php
3
7
8require_once $_SERVER['DOCUMENT_ROOT']. '/bitrix/modules/main/classes/general/tar_gz.php';
9
11 extends Main\IO\File
12 implements Translate\IErrorable
13{
14 // trait implements interface Translate\IErrorable
16
18 private $archive;
19
21 private $canUseCompression = false;
22
24 private $seekPath;
25
27 private $processedFileCount = 0;
28
35 public function __construct($path, $siteId = null)
36 {
37 parent::__construct($path, $siteId);
38
39 $this->canUseCompression = self::libAvailable();
40
41 $this->archive = new \CArchiver($this->getPhysicalPath(), $this->canUseCompression);
42 $this->archive->_strSeparator = '|';
43 }
44
50 public static function libAvailable()
51 {
52 return (\extension_loaded('zlib') || \function_exists('gzcompress'));
53 }
54
63 public function setOptions($options)
64 {
65 $this->archive->setOptions($options);
66 }
67
73 public function setSeekPosition($seekPath)
74 {
75 $this->seekPath = $seekPath;
76 }
77
82 public function getSeekPosition()
83 {
84 return $this->seekPath;
85 }
86
91 public function getProcessedFileCount()
92 {
93 return $this->processedFileCount;
94 }
95
96
105 public function pack(Translate\IO\Directory $directory, $seekPath = '')
106 {
107 $this->setOptions(array(
108 'ADD_PATH' => $directory->getName(),
109 'REMOVE_PATH' => $directory->getPhysicalPath(),
110 ));
111
112 if (empty($seekPath) && !empty($this->seekPath))
113 {
114 $seekPath = $this->seekPath;
115 }
116
117 $counter = \Closure::bind(
118 function()
119 {
120 if ($this instanceof \CArchiver)
121 {
123 return \count($this->lastFile) + ($this->tempres == "continue" ? 1 : 0);
124 }
125 return -1;
126 },
127 $this->archive,
128 '\CArchiver'
129 );
130
131 $res = $this->archive->pack([$directory->getPhysicalPath()], $seekPath);
132
133 switch ($res)
134 {
135 case \IBXArchive::StatusContinue:
136 $this->setSeekPosition($this->archive->getStartFile());
137 $this->processedFileCount = $counter();
138 break;
139
140 case \IBXArchive::StatusSuccess:
141 $this->processedFileCount = $counter();
142 break;
143
144 case \IBXArchive::StatusError:
145 $errors = $this->archive->getErrors();
146 if (count($errors) > 0)
147 {
148 foreach ($errors as $errorMessage)
149 {
150 $this->addError(new Main\Error($errorMessage[1], $errorMessage[0]));
151 }
152 }
153 break;
154 }
155
156 return $res;//($this->hasErrors() !== true);
157 }
158
166 public function extract(Translate\IO\Directory $target)
167 {
168 $unpack = \Closure::bind(
169 function($path)
170 {
171 if ($this instanceof \CArchiver)
172 {
174 $this->_arErrors = array();
175
176 $listDetail = array();
177
178 if ($result = $this->_openRead())
179 {
180 $result = $this->_extractList($path, $listDetail, 'complete', array(), '');
181 $this->_close();
182 }
183 if ($result)
184 {
185 return \count($listDetail);
186 }
187 }
188 return -1;
189 },
190 $this->archive,
191 '\CArchiver'
192 );
193
194 //$res = $this->archive->extractFiles($target->getPhysicalPath());
195 $res = $unpack($target->getPhysicalPath());
196
197 if ($res < 0)
198 {
199 $errors = $this->archive->getErrors();
200 if (\count($errors) > 0)
201 {
202 foreach ($errors as $errorMessage)
203 {
204 $this->addError(new Main\Error($errorMessage[1], $errorMessage[0]));
205 }
206 }
207 }
208 else
209 {
210 $this->processedFileCount = $res;
211 }
212
213 return ($this->hasErrors() !== true);
214 }
215}
$path
$siteId
getPhysicalPath()
pack(Translate\IO\Directory $directory, $seekPath='')
Definition archiver.php:105
__construct($path, $siteId=null)
Definition archiver.php:35
extract(Translate\IO\Directory $target)
Definition archiver.php:166
addError(Main\Error $error)