1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Amplexor\XConnect library |
4
|
|
|
* |
5
|
|
|
* @license http://opensource.org/licenses/MIT |
6
|
|
|
* @link https://github.com/amplexor-drupal/xconnect/ |
7
|
|
|
* @version 1.0.0 |
8
|
|
|
* @package Amplexor.XConnect |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Amplexor\XConnect\Response\File; |
15
|
|
|
|
16
|
|
|
use Amplexor\XConnect\Response\Info; |
17
|
|
|
use Amplexor\XConnect\Response\File\FileException; |
18
|
|
|
|
19
|
|
|
class ZipFile implements FileInterface |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The ZipArchive. |
23
|
|
|
* |
24
|
|
|
* @var \ZipArchive |
25
|
|
|
*/ |
26
|
|
|
private $zip; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The Info object retrieved from the ZipFile. |
30
|
|
|
* |
31
|
|
|
* @var Info |
32
|
|
|
*/ |
33
|
|
|
private $info; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The filepath where the file is opened from. |
37
|
|
|
* |
38
|
|
|
* @var string |
39
|
|
|
*/ |
40
|
|
|
private $filePath; |
41
|
|
|
|
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Create the object based on the file path. |
45
|
|
|
* |
46
|
|
|
* @param string $filePath |
47
|
|
|
* The full file path to the Zip file. |
48
|
|
|
* |
49
|
|
|
* @throws FileException |
50
|
|
|
* When the file can not be opened. |
51
|
|
|
*/ |
52
|
12 |
|
public function __construct($filePath) |
53
|
|
|
{ |
54
|
12 |
|
if (!is_readable($filePath)) { |
55
|
3 |
|
throw new FileException( |
56
|
3 |
|
sprintf( |
57
|
3 |
|
'File "%s" does not exists or is not readable.', |
58
|
|
|
$filePath |
59
|
3 |
|
) |
60
|
3 |
|
); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
// Create and open the archive. |
64
|
9 |
|
$zip = new \ZipArchive(); |
65
|
9 |
|
$result = $zip->open($filePath); |
66
|
|
|
|
67
|
9 |
|
if ($result !== true) { |
68
|
3 |
|
throw new FileException( |
69
|
3 |
|
sprintf( |
70
|
3 |
|
'Can\'t open file "%s".', |
71
|
|
|
$filePath |
72
|
3 |
|
) |
73
|
3 |
|
); |
74
|
|
|
} |
75
|
|
|
|
76
|
6 |
|
$this->zip = $zip; |
77
|
6 |
|
$this->filePath = $filePath; |
78
|
6 |
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Close the ZipArchive when the object is destructed. |
82
|
|
|
*/ |
83
|
6 |
|
public function __destruct() |
84
|
|
|
{ |
85
|
6 |
|
if ($this->zip) { |
86
|
6 |
|
$this->zip->close(); |
87
|
6 |
|
} |
88
|
6 |
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @inheritDoc |
92
|
|
|
*/ |
93
|
3 |
|
public function getInfo() |
94
|
|
|
{ |
95
|
3 |
|
if (!$this->info) { |
96
|
3 |
|
$fileName = basename($this->filePath); |
97
|
3 |
|
$infoName = preg_replace('/\.zip$/', '', $fileName) . '.xml'; |
98
|
|
|
|
99
|
3 |
|
$this->info = new Info( |
100
|
3 |
|
$this->getContent($infoName) |
101
|
3 |
|
); |
102
|
3 |
|
} |
103
|
|
|
|
104
|
3 |
|
return $this->info; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @inheritDoc |
109
|
|
|
*/ |
110
|
6 |
|
public function getContent($path) |
111
|
|
|
{ |
112
|
6 |
|
$content = ''; |
113
|
6 |
|
$fp = $this->zip->getStream($path); |
114
|
|
|
|
115
|
6 |
|
if (!$fp) { |
116
|
3 |
|
throw new FileException( |
117
|
3 |
|
sprintf('File "%s" not found in archive.', $path) |
118
|
3 |
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
3 |
|
while (!feof($fp)) { |
122
|
3 |
|
$content .= fread($fp, 2); |
123
|
3 |
|
} |
124
|
3 |
|
fclose($fp); |
125
|
|
|
|
126
|
3 |
|
return $content; |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|