|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
******************************************************* |
|
4
|
|
|
*** *** |
|
5
|
|
|
*** backpack *** |
|
6
|
|
|
*** Cedric MONTUY pour CHG-WEB *** |
|
7
|
|
|
*** Original author : Yoshi Sakai *** |
|
8
|
|
|
*** *** |
|
9
|
|
|
******************************************************* |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
class zipfile |
|
13
|
|
|
{ |
|
14
|
|
|
public $datasec = []; |
|
15
|
|
|
public $ctrl_dir = []; |
|
16
|
|
|
public $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; |
|
17
|
|
|
public $old_offset = 0; |
|
18
|
|
|
|
|
19
|
|
|
public function __construct() |
|
20
|
|
|
{ |
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
|
|
protected function unix2DosTime($unixtime = 0) |
|
24
|
|
|
{ |
|
25
|
|
|
$timearray = (0 == $unixtime) ? getdate() : getdate($unixtime); |
|
26
|
|
|
|
|
27
|
|
|
if ($timearray['year'] < 1980) { |
|
28
|
|
|
$timearray['year'] = 1980; |
|
29
|
|
|
$timearray['mon'] = 1; |
|
30
|
|
|
$timearray['mday'] = 1; |
|
31
|
|
|
$timearray['hours'] = 0; |
|
32
|
|
|
$timearray['minutes'] = 0; |
|
33
|
|
|
$timearray['seconds'] = 0; |
|
34
|
|
|
} // end if |
|
35
|
|
|
|
|
36
|
|
|
return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1); |
|
37
|
|
|
} // end of the 'unix2DosTime()' method |
|
38
|
|
|
|
|
39
|
|
|
public function addFile($data, $name, $time = 0) |
|
40
|
|
|
{ |
|
41
|
|
|
$name = str_replace('\\', '/', $name); |
|
42
|
|
|
|
|
43
|
|
|
$dtime = dechex($this->unix2DosTime($time)); |
|
44
|
|
|
$hexdtime = '\x' . $dtime[6] . $dtime[7] . '\x' . $dtime[4] . $dtime[5] . '\x' . $dtime[2] . $dtime[3] . '\x' . $dtime[0] . $dtime[1]; |
|
45
|
|
|
eval('$hexdtime = \'' . $hexdtime . '\';'); |
|
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
$fr = "\x50\x4b\x03\x04"; |
|
48
|
|
|
$fr .= "\x14\x00"; // ver needed to extract |
|
49
|
|
|
$fr .= "\x00\x00"; // gen purpose bit flag |
|
50
|
|
|
$fr .= "\x08\x00"; // compression method |
|
51
|
|
|
$fr .= $hexdtime; // last mod time and date |
|
52
|
|
|
|
|
53
|
|
|
// "local file header" segment |
|
54
|
|
|
$unc_len = strlen($data); |
|
55
|
|
|
$crc = crc32($data); |
|
56
|
|
|
$zdata = gzcompress($data); |
|
57
|
|
|
$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug |
|
58
|
|
|
$c_len = strlen($zdata); |
|
59
|
|
|
$fr .= pack('V', $crc); // crc32 |
|
60
|
|
|
$fr .= pack('V', $c_len); // compressed filesize |
|
61
|
|
|
$fr .= pack('V', $unc_len); // uncompressed filesize |
|
62
|
|
|
$fr .= pack('v', strlen($name)); // length of filename |
|
63
|
|
|
$fr .= pack('v', 0); // extra field length |
|
64
|
|
|
$fr .= $name; |
|
65
|
|
|
|
|
66
|
|
|
// "file data" segment |
|
67
|
|
|
$fr .= $zdata; |
|
68
|
|
|
|
|
69
|
|
|
// "data descriptor" segment (optional but necessary if archive is not |
|
70
|
|
|
// served as file) |
|
71
|
|
|
$fr .= pack('V', $crc); // crc32 |
|
72
|
|
|
$fr .= pack('V', $c_len); // compressed filesize |
|
73
|
|
|
$fr .= pack('V', $unc_len); // uncompressed filesize |
|
74
|
|
|
|
|
75
|
|
|
// add this entry to array |
|
76
|
|
|
$this->datasec[] = $fr; |
|
77
|
|
|
$new_offset = strlen(implode('', $this->datasec)); |
|
78
|
|
|
|
|
79
|
|
|
// now add to central directory record |
|
80
|
|
|
$cdrec = "\x50\x4b\x01\x02"; |
|
81
|
|
|
$cdrec .= "\x00\x00"; // version made by |
|
82
|
|
|
$cdrec .= "\x14\x00"; // version needed to extract |
|
83
|
|
|
$cdrec .= "\x00\x00"; // gen purpose bit flag |
|
84
|
|
|
$cdrec .= "\x08\x00"; // compression method |
|
85
|
|
|
$cdrec .= $hexdtime; // last mod time & date |
|
86
|
|
|
$cdrec .= pack('V', $crc); // crc32 |
|
87
|
|
|
$cdrec .= pack('V', $c_len); // compressed filesize |
|
88
|
|
|
$cdrec .= pack('V', $unc_len); // uncompressed filesize |
|
89
|
|
|
$cdrec .= pack('v', strlen($name)); // length of filename |
|
90
|
|
|
$cdrec .= pack('v', 0); // extra field length |
|
91
|
|
|
$cdrec .= pack('v', 0); // file comment length |
|
92
|
|
|
$cdrec .= pack('v', 0); // disk number start |
|
93
|
|
|
$cdrec .= pack('v', 0); // internal file attributes |
|
94
|
|
|
$cdrec .= pack('V', 32); // external file attributes - 'archive' bit set |
|
95
|
|
|
|
|
96
|
|
|
$cdrec .= pack('V', $this->old_offset); // relative offset of local header |
|
97
|
|
|
$this->old_offset = $new_offset; |
|
98
|
|
|
|
|
99
|
|
|
$cdrec .= $name; |
|
100
|
|
|
|
|
101
|
|
|
// optional extra field, file comment goes here |
|
102
|
|
|
// save to central directory |
|
103
|
|
|
$this->ctrl_dir[] = $cdrec; |
|
104
|
|
|
} // end of the 'addFile()' method |
|
105
|
|
|
|
|
106
|
|
|
public function file() |
|
107
|
|
|
{ |
|
108
|
|
|
$data = implode('', $this->datasec); |
|
109
|
|
|
$ctrldir = implode('', $this->ctrl_dir); |
|
110
|
|
|
|
|
111
|
|
|
return $data . $ctrldir . $this->eof_ctrl_dir . pack('v', count($this->ctrl_dir)) . // total # of entries "on this disk" |
|
112
|
|
|
pack('v', count($this->ctrl_dir)) . // total # of entries overall |
|
113
|
|
|
pack('V', strlen($ctrldir)) . // size of central dir |
|
114
|
|
|
pack('V', strlen($data)) . // offset to start of central dir |
|
115
|
|
|
"\x00\x00"; // .zip file comment length |
|
116
|
|
|
} // end of the 'file()' method |
|
117
|
|
|
} // end of the 'zipfile' class |
|
118
|
|
|
|