Translation update done using Pootle.
[phpmyadmin.git] / libraries / zip.lib.php
blob7a0094f0b52c59a24a66eafff0e11b3317163b4d
1 <?php
2 /* vim: set expandtab sw=4 ts=4 sts=4: */
3 /**
5 * @package PhpMyAdmin
6 */
8 /**
9 * Zip file creation class.
10 * Makes zip files.
12 * @see Official ZIP file format: http://www.pkware.com/support/zip-app-note
14 * @access public
15 * @package PhpMyAdmin
17 class zipfile
19 /**
20 * Whether to echo zip as it's built or return as string from -> file
22 * @var boolean $doWrite
24 var $doWrite = false;
26 /**
27 * Array to store compressed data
29 * @var array $datasec
31 var $datasec = array();
33 /**
34 * Central directory
36 * @var array $ctrl_dir
38 var $ctrl_dir = array();
40 /**
41 * End of central directory record
43 * @var string $eof_ctrl_dir
45 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
47 /**
48 * Last offset position
50 * @var integer $old_offset
52 var $old_offset = 0;
55 /**
56 * Sets member variable this -> doWrite to true
57 * - Should be called immediately after class instantiantion
58 * - If set to true, then ZIP archive are echo'ed to STDOUT as each
59 * file is added via this -> addfile(), and central directories are
60 * echoed to STDOUT on final call to this -> file(). Also,
61 * this -> file() returns an empty string so it is safe to issue a
62 * "echo $zipfile;" command
64 * @access public
66 * @return nothing
68 function setDoWrite()
70 $this -> doWrite = true;
71 } // end of the 'setDoWrite()' method
73 /**
74 * Converts an Unix timestamp to a four byte DOS date and time format (date
75 * in high two bytes, time in low two bytes allowing magnitude comparison).
77 * @param integer $unixtime the current Unix timestamp
79 * @return integer the current date in a four byte DOS format
81 * @access private
83 function unix2DosTime($unixtime = 0)
85 $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
87 if ($timearray['year'] < 1980) {
88 $timearray['year'] = 1980;
89 $timearray['mon'] = 1;
90 $timearray['mday'] = 1;
91 $timearray['hours'] = 0;
92 $timearray['minutes'] = 0;
93 $timearray['seconds'] = 0;
94 } // end if
96 return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
97 ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
98 } // end of the 'unix2DosTime()' method
102 * Adds "file" to archive
104 * @param string $data file contents
105 * @param string $name name of the file in the archive (may contains the path)
106 * @param integer $time the current timestamp
108 * @access public
110 * @return nothing
112 function addFile($data, $name, $time = 0)
114 $name = str_replace('\\', '/', $name);
116 $dtime = substr("00000000" . dechex($this->unix2DosTime($time)), -8);
117 $hexdtime = '\x' . $dtime[6] . $dtime[7]
118 . '\x' . $dtime[4] . $dtime[5]
119 . '\x' . $dtime[2] . $dtime[3]
120 . '\x' . $dtime[0] . $dtime[1];
121 eval('$hexdtime = "' . $hexdtime . '";');
123 $fr = "\x50\x4b\x03\x04";
124 $fr .= "\x14\x00"; // ver needed to extract
125 $fr .= "\x00\x00"; // gen purpose bit flag
126 $fr .= "\x08\x00"; // compression method
127 $fr .= $hexdtime; // last mod time and date
129 // "local file header" segment
130 $unc_len = strlen($data);
131 $crc = crc32($data);
132 $zdata = gzcompress($data);
133 $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
134 $c_len = strlen($zdata);
135 $fr .= pack('V', $crc); // crc32
136 $fr .= pack('V', $c_len); // compressed filesize
137 $fr .= pack('V', $unc_len); // uncompressed filesize
138 $fr .= pack('v', strlen($name)); // length of filename
139 $fr .= pack('v', 0); // extra field length
140 $fr .= $name;
142 // "file data" segment
143 $fr .= $zdata;
145 // echo this entry on the fly, ...
146 if ( $this -> doWrite) {
147 echo $fr;
148 } else { // ... OR add this entry to array
149 $this -> datasec[] = $fr;
152 // now add to central directory record
153 $cdrec = "\x50\x4b\x01\x02";
154 $cdrec .= "\x00\x00"; // version made by
155 $cdrec .= "\x14\x00"; // version needed to extract
156 $cdrec .= "\x00\x00"; // gen purpose bit flag
157 $cdrec .= "\x08\x00"; // compression method
158 $cdrec .= $hexdtime; // last mod time & date
159 $cdrec .= pack('V', $crc); // crc32
160 $cdrec .= pack('V', $c_len); // compressed filesize
161 $cdrec .= pack('V', $unc_len); // uncompressed filesize
162 $cdrec .= pack('v', strlen($name)); // length of filename
163 $cdrec .= pack('v', 0); // extra field length
164 $cdrec .= pack('v', 0); // file comment length
165 $cdrec .= pack('v', 0); // disk number start
166 $cdrec .= pack('v', 0); // internal file attributes
167 $cdrec .= pack('V', 32); // external file attributes - 'archive' bit set
169 $cdrec .= pack('V', $this -> old_offset); // relative offset of local header
170 $this -> old_offset += strlen($fr);
172 $cdrec .= $name;
174 // optional extra field, file comment goes here
175 // save to central directory
176 $this -> ctrl_dir[] = $cdrec;
177 } // end of the 'addFile()' method
181 * Echo central dir if ->doWrite==true, else build string to return
183 * @return string if ->doWrite {empty string} else the ZIP file contents
185 * @access public
187 function file()
189 $ctrldir = implode('', $this -> ctrl_dir);
190 $header = $ctrldir .
191 $this -> eof_ctrl_dir .
192 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
193 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
194 pack('V', strlen($ctrldir)) . // size of central dir
195 pack('V', $this -> old_offset) . // offset to start of central dir
196 "\x00\x00"; // .zip file comment length
198 if ( $this -> doWrite ) { // Send central directory & end ctrl dir to STDOUT
199 echo $header;
200 return ""; // Return empty string
201 } else { // Return entire ZIP archive as string
202 $data = implode('', $this -> datasec);
203 return $data . $header;
205 } // end of the 'file()' method
207 } // end of the 'zipfile' class