XOOPS 2.5.6  Final
 All Classes Namespaces Files Functions Variables Pages
class.zipfile.php
Go to the documentation of this file.
1 <?php
2 // $Id: class.zipfile.php 8066 2011-11-06 05:09:33Z beckmi $
32 class zipfile
33 {
39  var $datasec = array();
40 
46  var $ctrl_dir = array();
47 
53  var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
54 
60  var $old_offset = 0;
61 
70  function unix2DosTime($unixtime = 0)
71  {
72  $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
73  if ($timearray['year'] < 1980) {
74  $timearray['year'] = 1980;
75  $timearray['mon'] = 1;
76  $timearray['mday'] = 1;
77  $timearray['hours'] = 0;
78  $timearray['minutes'] = 0;
79  $timearray['seconds'] = 0;
80  } // end if
81  return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) | ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
82  } // end of the 'unix2DosTime()' method
91  function addFile($data, $name, $time = 0)
92  {
93  $name = str_replace('\\', '/', $name);
94 
95  $dtime = dechex($this->unix2DosTime($time));
96  $hexdtime = '\x' . $dtime[6] . $dtime[7] . '\x' . $dtime[4] . $dtime[5] . '\x' . $dtime[2] . $dtime[3] . '\x' . $dtime[0] . $dtime[1];
97  eval('$hexdtime = "' . $hexdtime . '";');
98 
99  $fr = "\x50\x4b\x03\x04";
100  $fr .= "\x14\x00"; // ver needed to extract
101  $fr .= "\x00\x00"; // gen purpose bit flag
102  $fr .= "\x08\x00"; // compression method
103  $fr .= $hexdtime; // last mod time and date
104  // "local file header" segment
105  $unc_len = strlen($data);
106  $crc = crc32($data);
107  $zdata = gzcompress($data);
108  $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
109  $c_len = strlen($zdata);
110  $fr .= pack('V', $crc); // crc32
111  $fr .= pack('V', $c_len); // compressed filesize
112  $fr .= pack('V', $unc_len); // uncompressed filesize
113  $fr .= pack('v', strlen($name)); // length of filename
114  $fr .= pack('v', 0); // extra field length
115  $fr .= $name;
116  // "file data" segment
117  $fr .= $zdata;
118  // "data descriptor" segment (optional but necessary if archive is not
119  // served as file)
120  $fr .= pack('V', $crc); // crc32
121  $fr .= pack('V', $c_len); // compressed filesize
122  $fr .= pack('V', $unc_len); // uncompressed filesize
123  // add this entry to array
124  $this->datasec[] = $fr;
125  $new_offset = strlen(implode('', $this->datasec));
126  // now add to central directory record
127  $cdrec = "\x50\x4b\x01\x02";
128  $cdrec .= "\x00\x00"; // version made by
129  $cdrec .= "\x14\x00"; // version needed to extract
130  $cdrec .= "\x00\x00"; // gen purpose bit flag
131  $cdrec .= "\x08\x00"; // compression method
132  $cdrec .= $hexdtime; // last mod time & date
133  $cdrec .= pack('V', $crc); // crc32
134  $cdrec .= pack('V', $c_len); // compressed filesize
135  $cdrec .= pack('V', $unc_len); // uncompressed filesize
136  $cdrec .= pack('v', strlen($name)); // length of filename
137  $cdrec .= pack('v', 0); // extra field length
138  $cdrec .= pack('v', 0); // file comment length
139  $cdrec .= pack('v', 0); // disk number start
140  $cdrec .= pack('v', 0); // internal file attributes
141  $cdrec .= pack('V', 32); // external file attributes - 'archive' bit set
142  $cdrec .= pack('V', $this->old_offset); // relative offset of local header
143  $this->old_offset = $new_offset;
144  $cdrec .= $name;
145  // optional extra field, file comment goes here
146  // save to central directory
147  $this->ctrl_dir[] = $cdrec;
148  } // end of the 'addFile()' method
155  function file()
156  {
157  $data = implode('', $this->datasec);
158  $ctrldir = implode('', $this->ctrl_dir);
159 
160  return $data . $ctrldir . $this->eof_ctrl_dir . pack('v', count($this->ctrl_dir)) . // total # of entries "on this disk"
161 pack('v', count($this->ctrl_dir)) . // total # of entries overall
162 pack('V', strlen($ctrldir)) . // size of central dir
163 pack('V', strlen($data)) . // offset to start of central dir
164 "\x00\x00"; // .zip file comment length
165  } // end of the 'file()' method
166 } // end of the 'zipfile' class
167 
168 
169 ?>