-
Notifications
You must be signed in to change notification settings - Fork 1
/
zip.php
315 lines (296 loc) · 8.13 KB
/
zip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
<?php
/**
ZIP archive utility for the Fat-Free Framework
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2009-2012 F3::Factory
Bong Cosca <[email protected]>
@package Zip
@version 2.0.12
**/
//! Utility class for ZIP archives
class Zip extends Base {
//@{ Locale-specific error/exception messages
const
TEXT_Required='A ZIP archive must be specified',
TEXT_NotValid='File %s is not a valid ZIP archive',
TEXT_UnMethod='Unsupported compression method';
//@}
//@{ ZIP header signatures
const
LFHDR_Sig='504B0304',
CDHDR_Sig='504B0102',
CDEND_Sig='504B0506';
//@}
const
//! Read-granularity of ZIP archive
BLOCK_Size=4096;
private
//! ZIP file name
$file,
//! Central directory container
$cdir,
//! Central directory relative offset
$cofs;
/**
Return central directory structure
@return array
@public
**/
function dir() {
return $this->cdir;
}
/**
Return content of specified file from ZIP archive; FALSE if
compression method is not supported
@return mixed
@param $path string
@public
**/
function get($path) {
if (!$path || $path[strlen($path)-1]=='/')
return FALSE;
$chdr=$this->cdir[$path];
// Find local file header
$zip=fopen($this->file,'rb');
fseek($zip,implode('',unpack('V',substr($chdr,42,4))));
// Read local file header
$fhdr=fread($zip,30+strlen($path));
$comp=self::binhex(substr($fhdr,8,2));
if ($comp!='0800' && $comp!='0000') {
trigger_error(self::TEXT_UnMethod);
return FALSE;
}
if ($len=implode(unpack('v',substr($fhdr,28,2))))
// Append extra field
$fhdr.=fread($zip,$len);
$len=unpack('V',substr($fhdr,22,4));
$data='';
if ($len)
$data=fread($zip,implode('',$len));
fclose($zip);
return hexdec($comp) && $data?gzinflate($data):$data;
}
/**
Add or replace file in ZIP archive using specified content;
Create folder if content is NULL or unspecified
@param $path string
@param $data string
@param $time integer
@public
**/
function set($path,$data=NULL,$time=0) {
$this->parse('set',$path,$data,$time);
}
/**
Delete file from ZIP archive
@param $path string
@public
**/
function clear($path) {
$this->parse('clear',$path);
}
/**
Parse ZIP archive
@param $path string
@param $func mixed
@public
**/
private function parse($action,$path,$data=NULL,$time=0) {
if (!$time)
$time=time();
$tfn=self::$vars['TEMP'].$_SERVER['SERVER_NAME'].'.zip.'.
self::hash($path);
$tmp=fopen($tfn,'wb+');
if (is_file($this->file)) {
$zip=fopen($this->file,'rb');
// Copy data from ZIP archive to temporary file
foreach ($this->cdir as $name=>$chdr)
if ($name!=$path) {
// Find local file header
fseek($zip,implode('',
unpack('V',substr($chdr,42,4))));
$fhdr=fread($zip,30+strlen($name));
$len=implode(unpack('v',substr($fhdr,28,2)));
if ($len)
// Append extra field
$fhdr.=fread($zip,$len);
// Update relative offset
$this->cdir[$name]=substr_replace(
$this->cdir[$name],pack('V',ftell($tmp)),42,4);
// Copy header and compressed content
$len=implode('',unpack('V',substr($fhdr,18,4)));
fwrite($tmp,$fhdr.($len?fread($zip,$len):''));
}
fclose($zip);
}
switch ($action) {
case 'set':
$path=self::fixslashes($path).
(is_null($data) && $path[strlen($path)]!='/'?'/':'');
$chdr=&$this->cdir[$path];
// Blank headers
$fhdr=str_repeat(chr(0),30).$path;
$chdr=str_repeat(chr(0),46).$path;
// Signatures
$fhdr=substr_replace(
$fhdr,self::hexbin(self::LFHDR_Sig),0,4);
$chdr=substr_replace(
$chdr,self::hexbin(self::CDHDR_Sig),0,4);
// Version needed to extract
$ver=self::hexbin(is_null($data)?'0A00':'1400');
$fhdr=substr_replace($fhdr,$ver,4,2);
$chdr=substr_replace($chdr,$ver,6,2);
// Last modification time
$mod=pack('V',self::unix2dostime($time));
$fhdr=substr_replace($fhdr,$mod,10,4);
$chdr=substr_replace($chdr,$mod,12,4);
// File name length
$len=pack('v',strlen($path));
$fhdr=substr_replace($fhdr,$len,26,2);
$chdr=substr_replace($chdr,$len,28,2);
// File header relative offset
$chdr=substr_replace(
$chdr,pack('V',ftell($tmp)),42,4);
if (!is_null($data)) {
// Compress data/Fix CRC bug
$comp=gzdeflate($data);
// Compression method
$def=self::hexbin('0800');
$fhdr=substr_replace($fhdr,$def,8,2);
$chdr=substr_replace($chdr,$def,10,2);
// CRC32
$crc=pack('V',crc32($data));
$fhdr=substr_replace($fhdr,$crc,14,4);
$chdr=substr_replace($chdr,$crc,16,4);
// Compressed size
$size=pack('V',strlen($comp));
$fhdr=substr_replace($fhdr,$size,18,4);
$chdr=substr_replace($chdr,$size,20,4);
// Uncompressed size
$size=pack('V',strlen($data));
$fhdr=substr_replace($fhdr,$size,22,4);
$chdr=substr_replace($chdr,$size,24,4);
// Copy header and compressed content
fwrite($tmp,$fhdr.$comp);
}
break;
case 'clear':
$path=self::fixslashes($path);
unset($this->cdir[$path]);
break;
}
// Central directory relative offset
$this->cofs=ftell($tmp);
foreach ($this->cdir as $raw)
// Copy central directory file headers
fwrite($tmp,$raw);
// Blank end of central directory record
$cend=str_repeat(chr(0),22);
// Signature
$cend=substr_replace($cend,self::hexbin(self::CDEND_Sig),0,4);
// Total number of central directory records
$total=pack('v',count($this->cdir));
$cend=substr_replace($cend,$total,8,2);
$cend=substr_replace($cend,$total,10,2);
// Size of central directory
$cend=substr_replace(
$cend,pack('V',strlen(implode('',$this->cdir))),12,4);
// Relative offset of central directory
$cend=substr_replace($cend,pack('V',$this->cofs),16,4);
fwrite($tmp,$cend);
fclose($tmp);
if (is_file($this->file))
// Delete old ZIP archive
unlink($this->file);
rename($tfn,$this->file);
}
/**
Convert 4-byte DOS time to Un*x timestamp
@return integer
@param $time integer
@public
**/
static function dos2unixtime($time) {
$date=$time>>16;
return mktime(
($time & 0xF800)>>11,
($time & 0x07E0)>>5,
($time & 0x001F)<<1,
($date & 0x01E0)>>5,
($date & 0x001F),
(($date & 0xFE00)>>9)+1980
);
}
/**
Convert Un*x timestamp to 4-byte DOS time
@return integer
@param $time integer
@public
**/
static function unix2dostime($time=0) {
$time=$time?getdate($time):getdate();
if ($time['year']<1980)
$time=array_combine(
array('hours','minutes','seconds','mon','mday','year'),
array(0,0,0,1,1,1980)
);
return
($time['hours']<<11) |
($time['minutes']<<5) |
($time['seconds']>>1) |
($time['mon']<<21) |
($time['mday']<<16) |
(($time['year']-1980)<<25);
}
/**
Class constructor
@param $path string
@public
**/
function __construct($path=NULL) {
if (is_null($path)) {
trigger_error(self::TEXT_Required);
return;
}
$path=self::resolve($path);
$this->file=$path;
$this->cdir=array();
if (!is_file($path))
return;
// Parse file contents
$zip=fopen($path,'rb');
$found=FALSE;
$cdir='';
while (!feof($zip)) {
$cdir.=fread($zip,self::BLOCK_Size);
if (is_bool($found)) {
$found=strstr($cdir,self::hexbin(self::CDHDR_Sig));
if (is_string($found)) {
// Start of central directory record
$cdir=$found;
$this->cofs=ftell($zip)-strlen($found);
}
elseif (strlen($cdir)>self::BLOCK_Size)
// Conserve memory
$cdir=substr($cdir,self::BLOCK_Size);
}
}
fclose($zip);
if (is_bool(strstr($cdir,self::hexbin(self::CDEND_Sig)))) {
// Invalid ZIP archive
trigger_error(sprintf(self::TEXT_NotValid,$path));
return;
}
// Save central directory record
foreach (array_slice(explode(self::hexbin(self::CDHDR_Sig),
strstr($cdir,self::hexbin(self::CDEND_Sig),TRUE)),1)
as $raw)
// Extract name and use as array key
$this->cdir[substr(
$raw,42,implode('',unpack('v',substr($raw,24,2)))
)]=self::hexbin(self::CDHDR_Sig).$raw;
}
}