1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Jaeger |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, mithra62 |
6
|
|
|
* @link http://jaeger-app.com |
7
|
|
|
* @version 1.0 |
8
|
|
|
* @filesource ./Compress.php |
9
|
|
|
*/ |
10
|
|
|
namespace JaegerApp; |
11
|
|
|
|
12
|
|
|
use JaegerApp\Exceptions\CompressException; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Jaeger - Compress Object |
16
|
|
|
* |
17
|
|
|
* Handles compressing and decompressing files into various formats |
18
|
|
|
* |
19
|
|
|
* @package Compression |
20
|
|
|
* @author Eric Lamb <[email protected]> |
21
|
|
|
*/ |
22
|
|
|
class Compress |
23
|
|
|
{ |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* Returns an instance of the archive object |
27
|
|
|
* |
28
|
|
|
* @var \ZipArchive |
29
|
|
|
*/ |
30
|
|
|
protected $archiver = null; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Flag to determine whether the orignial file should be kept |
34
|
|
|
* |
35
|
|
|
* @var bool |
36
|
|
|
*/ |
37
|
|
|
protected $keep_original = true; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* The name of the completed archive file |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $archive_name = 'archive.zip'; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Returns an instance of the Archiver |
48
|
|
|
* |
49
|
|
|
* @return ZipArchive |
50
|
|
|
*/ |
51
|
|
|
public function getArchiver() |
52
|
|
|
{ |
53
|
|
|
if (is_null($this->archiver)) { |
54
|
|
|
$this->archiver = new \ZipArchive(); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return $this->archiver; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Extracts a compressed file to the destination |
62
|
|
|
* |
63
|
|
|
* @param string $file |
64
|
|
|
* The full path to the file to extract |
65
|
|
|
* @param string $destination |
66
|
|
|
* The destination to extract to |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function extract($file, $destination = false) |
70
|
|
|
{ |
71
|
|
|
$archive = $this->getArchiver()->open($file); |
72
|
|
|
if ($archive) { |
73
|
|
|
$this->getArchiver()->extractTo($destination); |
74
|
|
|
return $this->getArchiver()->close(); |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Sets the archive file name |
80
|
|
|
* |
81
|
|
|
* @param string $name |
82
|
|
|
* @return \mithra62\Compress |
83
|
|
|
*/ |
84
|
|
|
public function setArchiveName($name) |
85
|
|
|
{ |
86
|
|
|
$this->archive_name = $name . '.zip'; |
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Returns the archive file name |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public function getArchiveName() |
96
|
|
|
{ |
97
|
|
|
return $this->archive_name; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Starts the process of creating an archive |
102
|
|
|
* |
103
|
|
|
* @param string $name |
104
|
|
|
* @return \mithra62\Compress |
105
|
|
|
*/ |
106
|
|
|
public function create($name) |
107
|
|
|
{ |
108
|
|
|
$this->setArchiveName($name); |
109
|
|
|
$this->getArchiver()->open($this->getArchiveName(), \ZipArchive::CREATE | \ZipArchive::OVERWRITE); |
110
|
|
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Adds a file to the archive |
115
|
|
|
* |
116
|
|
|
* @param string $path |
117
|
|
|
* @param string $relative |
118
|
|
|
* @return \mithra62\Compress |
119
|
|
|
*/ |
120
|
|
|
public function add($path, $relative) |
121
|
|
|
{ |
122
|
|
|
$this->getArchiver()->addFile($path, $relative); |
123
|
|
|
return $this; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Closes the zip archive object |
128
|
|
|
* |
129
|
|
|
* @return string The path to the archive |
130
|
|
|
*/ |
131
|
|
|
public function close() |
132
|
|
|
{ |
133
|
|
|
$this->getArchiver()->close(); |
134
|
|
|
return $this->getArchiveName(); |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* Compresses a single file |
139
|
|
|
* |
140
|
|
|
* @param string $file |
141
|
|
|
* the full path to the file to compress |
142
|
|
|
* @param string $desination |
143
|
|
|
* optional the full path to the destination. If none is given then $file is used with the extension appended |
144
|
|
|
*/ |
145
|
|
|
public function archiveSingle($file, $desination = false) |
146
|
|
|
{ |
147
|
|
|
if ($file == '') { |
148
|
|
|
throw new CompressException('__exception_compress_file_value_empty'); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
if (! file_exists($file)) { |
152
|
|
|
throw new CompressException('__exception_compress_file_not_exist'); |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
if (! is_readable($file)) { |
156
|
|
|
throw new CompressException('__exception_compress_file_not_readable'); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
// work out path stuff |
160
|
|
|
$old_cwd = getcwd(); |
161
|
|
|
$path = dirname($file); |
162
|
|
|
chdir($path); |
163
|
|
|
|
164
|
|
|
$name = $this->getArchiveName(); |
165
|
|
|
$zip = $this->getArchiver(); |
166
|
|
|
$zip->open($name, \ZipArchive::CREATE); |
167
|
|
|
if (file_exists($file)) { |
168
|
|
|
$zip->addFile(basename($file)); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($zip->status != '0') { |
172
|
|
|
throw new CompressException('__exception_compression'); |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
$zip->close(); |
176
|
|
|
|
177
|
|
|
if (! $this->getKeepOriginal() && file_exists($file)) { |
178
|
|
|
unlink($file); |
179
|
|
|
} |
180
|
|
|
|
181
|
|
|
if ($desination) { |
|
|
|
|
182
|
|
|
$desination = realpath($desination) . DIRECTORY_SEPARATOR . $name; |
183
|
|
|
copy($name, $desination); |
184
|
|
|
unlink($name); |
185
|
|
|
|
186
|
|
|
$name = $desination; |
187
|
|
|
} else { |
188
|
|
|
$name = $path . DIRECTORY_SEPARATOR . $name; |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
// reset path |
192
|
|
|
chdir($old_cwd); |
193
|
|
|
|
194
|
|
|
return $name; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Sets whether the original file should be removed once compressed |
199
|
|
|
* |
200
|
|
|
* @param bool $flag |
201
|
|
|
* @return \mithra62\Compress |
202
|
|
|
*/ |
203
|
|
|
public function setKeepOriginal($flag = true) |
204
|
|
|
{ |
205
|
|
|
$this->keep_original = $flag; |
206
|
|
|
return $this; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
/** |
210
|
|
|
* Returns whether the original file should be kept once compressed |
211
|
|
|
* |
212
|
|
|
* @return \mithra62\bool |
213
|
|
|
*/ |
214
|
|
|
public function getKeepOriginal() |
215
|
|
|
{ |
216
|
|
|
return $this->keep_original; |
217
|
|
|
} |
218
|
|
|
} |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: