|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Velikonja\LabbyBundle\Util; |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* Class ZipArchive |
|
7
|
|
|
* |
|
8
|
|
|
* @package Velikonja\LabbyBundle\Util |
|
9
|
|
|
*/ |
|
10
|
|
|
class ZipArchive |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var string |
|
14
|
|
|
*/ |
|
15
|
|
|
private $tmpDir; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var string |
|
19
|
|
|
*/ |
|
20
|
|
|
private $compressedFileName; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param null|string $tmpDir |
|
24
|
|
|
* |
|
25
|
|
|
* @throws \Exception |
|
26
|
|
|
*/ |
|
27
|
14 |
|
public function __construct($tmpDir = null) |
|
28
|
|
|
{ |
|
29
|
14 |
|
$this->tmpDir = $tmpDir ?: sys_get_temp_dir(); |
|
30
|
14 |
|
$this->compressedFileName = 'dump.sql'; |
|
31
|
|
|
|
|
32
|
14 |
|
if (! is_writable($this->tmpDir)) { |
|
33
|
1 |
|
throw new \Exception( |
|
34
|
1 |
|
sprintf('Temporary directory `%s` is unreadable.', $this->tmpDir) |
|
35
|
|
|
); |
|
36
|
|
|
} |
|
37
|
13 |
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @param string $filePath |
|
41
|
|
|
* |
|
42
|
|
|
* @return bool |
|
43
|
|
|
*/ |
|
44
|
3 |
|
public function isZipped($filePath) |
|
45
|
|
|
{ |
|
46
|
3 |
|
$fileInfo = finfo_open(FILEINFO_MIME_TYPE); |
|
47
|
3 |
|
$type = finfo_file($fileInfo, $filePath); |
|
48
|
|
|
|
|
49
|
3 |
|
finfo_close($fileInfo); |
|
50
|
|
|
|
|
51
|
3 |
|
return 'application/zip' === $type; |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @param string $filePath |
|
56
|
|
|
* |
|
57
|
|
|
* @return string |
|
58
|
|
|
*/ |
|
59
|
5 |
|
public function unzip($filePath) |
|
60
|
|
|
{ |
|
61
|
5 |
|
$tmpFile = $this->tmpDir . '/labby_' . uniqid() . '.sql'; |
|
62
|
|
|
|
|
63
|
5 |
|
$zip = new \ZipArchive(); |
|
64
|
5 |
|
$zip->open($filePath); |
|
65
|
|
|
|
|
66
|
5 |
|
if (false === $zip->locateName($this->compressedFileName)) { |
|
67
|
1 |
|
$zip->close(); |
|
68
|
|
|
|
|
69
|
1 |
|
throw new \InvalidArgumentException( |
|
70
|
|
|
sprintf( |
|
71
|
1 |
|
'File `%s` not found in ZIP archive `%s`.', |
|
72
|
1 |
|
$this->compressedFileName, |
|
73
|
|
|
$filePath |
|
74
|
|
|
) |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
4 |
|
$zip->extractTo($this->tmpDir, $this->compressedFileName); |
|
79
|
4 |
|
$zip->close(); |
|
80
|
|
|
|
|
81
|
4 |
|
rename($this->tmpDir . '/' . $this->compressedFileName, $tmpFile); |
|
82
|
|
|
|
|
83
|
4 |
|
return $tmpFile; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $path |
|
88
|
|
|
* @param string $content |
|
89
|
|
|
* |
|
90
|
|
|
* @throws \Exception |
|
91
|
|
|
*/ |
|
92
|
2 |
|
public function zip($path, $content) |
|
93
|
|
|
{ |
|
94
|
2 |
|
$zip = new \ZipArchive(); |
|
95
|
2 |
|
$zip->open($path, \ZipArchive::CREATE); |
|
96
|
|
|
|
|
97
|
2 |
|
$res = $zip->addFromString($this->compressedFileName, $content); |
|
98
|
|
|
|
|
99
|
2 |
|
if (! $res) { |
|
100
|
|
|
throw new \Exception(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
2 |
|
$zip->close(); |
|
104
|
2 |
|
} |
|
105
|
|
|
} |
|
106
|
|
|
|