1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Giansalex |
5
|
|
|
* Date: 16/07/2017 |
6
|
|
|
* Time: 13:00. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Greenter\Zip; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class ZipFile. |
13
|
|
|
*/ |
14
|
|
|
class ZipFile |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Array to store compressed data. |
18
|
|
|
* |
19
|
|
|
* @private array $datasec |
20
|
|
|
*/ |
21
|
|
|
private $datasec = []; |
22
|
|
|
/** |
23
|
|
|
* Central directory. |
24
|
|
|
* |
25
|
|
|
* @private array $ctrl_dir |
26
|
|
|
*/ |
27
|
|
|
private $ctrl_dir = []; |
28
|
|
|
/** |
29
|
|
|
* End of central directory record. |
30
|
|
|
* |
31
|
|
|
* @private string $eof_ctrl_dir |
32
|
|
|
*/ |
33
|
|
|
private $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00"; |
34
|
|
|
/** |
35
|
|
|
* Last offset position. |
36
|
|
|
* |
37
|
|
|
* @private integer $old_offset |
38
|
|
|
*/ |
39
|
|
|
private $old_offset = 0; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Converts an Unix timestamp to a four byte DOS date and time format (date |
43
|
|
|
* in high two bytes, time in low two bytes allowing magnitude comparison). |
44
|
|
|
* |
45
|
|
|
* @param int $unixtime the current Unix timestamp |
46
|
|
|
* |
47
|
|
|
* @return int the current date in a four byte DOS format |
48
|
|
|
*/ |
49
|
46 |
|
public function unix2DosTime($unixtime = 0) |
50
|
|
|
{ |
51
|
46 |
|
$timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); |
52
|
46 |
|
if ($timearray['year'] < 1980) { |
53
|
2 |
|
$timearray['year'] = 1980; |
54
|
2 |
|
$timearray['mon'] = 1; |
55
|
2 |
|
$timearray['mday'] = 1; |
56
|
2 |
|
$timearray['hours'] = 0; |
57
|
2 |
|
$timearray['minutes'] = 0; |
58
|
2 |
|
$timearray['seconds'] = 0; |
59
|
2 |
|
} // end if |
60
|
46 |
|
return (($timearray['year'] - 1980) << 25) |
61
|
46 |
|
| ($timearray['mon'] << 21) |
62
|
46 |
|
| ($timearray['mday'] << 16) |
63
|
46 |
|
| ($timearray['hours'] << 11) |
64
|
46 |
|
| ($timearray['minutes'] << 5) |
65
|
46 |
|
| ($timearray['seconds'] >> 1); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
// end of the 'unix2DosTime()' method |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Adds "file" to archive. |
72
|
|
|
* |
73
|
|
|
* @param string $data file contents |
74
|
|
|
* @param string $name name of the file in the archive (may contains the path) |
75
|
|
|
* @param int $time the current timestamp |
76
|
|
|
*/ |
77
|
44 |
|
public function addFile($data, $name, $time = 0) |
78
|
|
|
{ |
79
|
44 |
|
$name = str_replace('\\', '/', $name); |
80
|
44 |
|
$hexdtime = pack('V', $this->unix2DosTime($time)); |
81
|
44 |
|
$frd = "\x50\x4b\x03\x04"; |
82
|
44 |
|
$frd .= "\x14\x00"; // ver needed to extract |
83
|
44 |
|
$frd .= "\x00\x00"; // gen purpose bit flag |
84
|
44 |
|
$frd .= "\x08\x00"; // compression method |
85
|
44 |
|
$frd .= $hexdtime; // last mod time and date |
86
|
|
|
// "local file header" segment |
87
|
44 |
|
$unc_len = strlen($data); |
88
|
44 |
|
$crc = crc32($data); |
89
|
44 |
|
$zdata = gzcompress($data); |
90
|
44 |
|
$zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug |
91
|
44 |
|
$c_len = strlen($zdata); |
92
|
44 |
|
$frd .= pack('V', $crc); // crc32 |
93
|
44 |
|
$frd .= pack('V', $c_len); // compressed filesize |
94
|
44 |
|
$frd .= pack('V', $unc_len); // uncompressed filesize |
95
|
44 |
|
$frd .= pack('v', strlen($name)); // length of filename |
96
|
44 |
|
$frd .= pack('v', 0); // extra field length |
97
|
44 |
|
$frd .= $name; |
98
|
|
|
// "file data" segment |
99
|
44 |
|
$frd .= $zdata; |
100
|
|
|
// echo this entry on the fly, ... |
101
|
44 |
|
$this->datasec[] = $frd; |
102
|
|
|
// now add to central directory record |
103
|
44 |
|
$cdrec = "\x50\x4b\x01\x02"; |
104
|
44 |
|
$cdrec .= "\x00\x00"; // version made by |
105
|
44 |
|
$cdrec .= "\x14\x00"; // version needed to extract |
106
|
44 |
|
$cdrec .= "\x00\x00"; // gen purpose bit flag |
107
|
44 |
|
$cdrec .= "\x08\x00"; // compression method |
108
|
44 |
|
$cdrec .= $hexdtime; // last mod time & date |
109
|
44 |
|
$cdrec .= pack('V', $crc); // crc32 |
110
|
44 |
|
$cdrec .= pack('V', $c_len); // compressed filesize |
111
|
44 |
|
$cdrec .= pack('V', $unc_len); // uncompressed filesize |
112
|
44 |
|
$cdrec .= pack('v', strlen($name)); // length of filename |
113
|
44 |
|
$cdrec .= pack('v', 0); // extra field length |
114
|
44 |
|
$cdrec .= pack('v', 0); // file comment length |
115
|
44 |
|
$cdrec .= pack('v', 0); // disk number start |
116
|
44 |
|
$cdrec .= pack('v', 0); // internal file attributes |
117
|
44 |
|
$cdrec .= pack('V', 32); // external file attributes |
118
|
|
|
// - 'archive' bit set |
119
|
44 |
|
$cdrec .= pack('V', $this->old_offset); // relative offset of local header |
120
|
44 |
|
$this->old_offset += strlen($frd); |
121
|
44 |
|
$cdrec .= $name; |
122
|
|
|
// optional extra field, file comment goes here |
123
|
|
|
// save to central directory |
124
|
44 |
|
$this->ctrl_dir[] = $cdrec; |
125
|
44 |
|
} |
126
|
|
|
|
127
|
|
|
// end of the 'addFile()' method |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Echo central dir if ->doWrite==true, else build string to return. |
131
|
|
|
* |
132
|
|
|
* @return string if ->doWrite {empty string} else the ZIP file contents |
133
|
|
|
*/ |
134
|
44 |
|
public function file() |
135
|
|
|
{ |
136
|
44 |
|
$ctrldir = implode('', $this->ctrl_dir); |
137
|
|
|
$header = $ctrldir. |
138
|
44 |
|
$this->eof_ctrl_dir. |
139
|
44 |
|
pack('v', sizeof($this->ctrl_dir)). //total #of entries "on this disk" |
140
|
44 |
|
pack('v', sizeof($this->ctrl_dir)). //total #of entries overall |
141
|
44 |
|
pack('V', strlen($ctrldir)). //size of central dir |
142
|
44 |
|
pack('V', $this->old_offset). //offset to start of central dir |
143
|
44 |
|
"\x00\x00"; //.zip file comment length |
144
|
|
|
|
145
|
|
|
// Return entire ZIP archive as string |
146
|
44 |
|
$data = implode('', $this->datasec); |
147
|
|
|
|
148
|
44 |
|
return $data.$header; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
// end of the 'file()' method |
152
|
|
|
} |
153
|
|
|
|