Translation update done using Pootle.
[phpmyadmin/dkf.git] / libraries / zip.lib.php
blobc6ac6e5098d45fcec9d9c0eea7210f52fac4e8c5
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 * Based on :
14 * http://www.zend.com/codex.php?id=535&single=1
15 * By Eric Mueller <eric@themepark.com>
17 * http://www.zend.com/codex.php?id=470&single=1
18 * by Denis125 <webmaster@atlant.ru>
20 * a patch from Peter Listiak <mlady@users.sourceforge.net> for last modified
21 * date and time of the compressed file
23 * Official ZIP file format: http://www.pkware.com/appnote.txt
25 * @access public
26 * @package phpMyAdmin
28 class zipfile
30 /**
31 * Array to store compressed data
33 * @var array $datasec
35 var $datasec = array();
37 /**
38 * Central directory
40 * @var array $ctrl_dir
42 var $ctrl_dir = array();
44 /**
45 * End of central directory record
47 * @var string $eof_ctrl_dir
49 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
51 /**
52 * Last offset position
54 * @var integer $old_offset
56 var $old_offset = 0;
59 /**
60 * Converts an Unix timestamp to a four byte DOS date and time format (date
61 * in high two bytes, time in low two bytes allowing magnitude comparison).
63 * @param integer the current Unix timestamp
65 * @return integer the current date in a four byte DOS format
67 * @access private
69 function unix2DosTime($unixtime = 0) {
70 $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
72 if ($timearray['year'] < 1980) {
73 $timearray['year'] = 1980;
74 $timearray['mon'] = 1;
75 $timearray['mday'] = 1;
76 $timearray['hours'] = 0;
77 $timearray['minutes'] = 0;
78 $timearray['seconds'] = 0;
79 } // end if
81 return (($timearray['year'] - 1980) << 25) | ($timearray['mon'] << 21) | ($timearray['mday'] << 16) |
82 ($timearray['hours'] << 11) | ($timearray['minutes'] << 5) | ($timearray['seconds'] >> 1);
83 } // end of the 'unix2DosTime()' method
86 /**
87 * Adds "file" to archive
89 * @param string file contents
90 * @param string name of the file in the archive (may contains the path)
91 * @param integer the current timestamp
93 * @access public
95 function addFile($data, $name, $time = 0)
97 $name = str_replace('\\', '/', $name);
99 $dtime = dechex($this->unix2DosTime($time));
100 $hexdtime = '\x' . $dtime[6] . $dtime[7]
101 . '\x' . $dtime[4] . $dtime[5]
102 . '\x' . $dtime[2] . $dtime[3]
103 . '\x' . $dtime[0] . $dtime[1];
104 eval('$hexdtime = "' . $hexdtime . '";');
106 $fr = "\x50\x4b\x03\x04";
107 $fr .= "\x14\x00"; // ver needed to extract
108 $fr .= "\x00\x00"; // gen purpose bit flag
109 $fr .= "\x08\x00"; // compression method
110 $fr .= $hexdtime; // last mod time and date
112 // "local file header" segment
113 $unc_len = strlen($data);
114 $crc = crc32($data);
115 $zdata = gzcompress($data);
116 $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
117 $c_len = strlen($zdata);
118 $fr .= pack('V', $crc); // crc32
119 $fr .= pack('V', $c_len); // compressed filesize
120 $fr .= pack('V', $unc_len); // uncompressed filesize
121 $fr .= pack('v', strlen($name)); // length of filename
122 $fr .= pack('v', 0); // extra field length
123 $fr .= $name;
125 // "file data" segment
126 $fr .= $zdata;
128 // "data descriptor" segment (optional but necessary if archive is not
129 // served as file)
130 // this seems not to be needed at all and causes
131 // problems in some cases (bug #1037737)
132 //$fr .= pack('V', $crc); // crc32
133 //$fr .= pack('V', $c_len); // compressed filesize
134 //$fr .= pack('V', $unc_len); // uncompressed filesize
136 // add this entry to array
137 $this -> datasec[] = $fr;
139 // now add to central directory record
140 $cdrec = "\x50\x4b\x01\x02";
141 $cdrec .= "\x00\x00"; // version made by
142 $cdrec .= "\x14\x00"; // version needed to extract
143 $cdrec .= "\x00\x00"; // gen purpose bit flag
144 $cdrec .= "\x08\x00"; // compression method
145 $cdrec .= $hexdtime; // last mod time & date
146 $cdrec .= pack('V', $crc); // crc32
147 $cdrec .= pack('V', $c_len); // compressed filesize
148 $cdrec .= pack('V', $unc_len); // uncompressed filesize
149 $cdrec .= pack('v', strlen($name)); // length of filename
150 $cdrec .= pack('v', 0); // extra field length
151 $cdrec .= pack('v', 0); // file comment length
152 $cdrec .= pack('v', 0); // disk number start
153 $cdrec .= pack('v', 0); // internal file attributes
154 $cdrec .= pack('V', 32); // external file attributes - 'archive' bit set
156 $cdrec .= pack('V', $this -> old_offset); // relative offset of local header
157 $this -> old_offset += strlen($fr);
159 $cdrec .= $name;
161 // optional extra field, file comment goes here
162 // save to central directory
163 $this -> ctrl_dir[] = $cdrec;
164 } // end of the 'addFile()' method
168 * Dumps out file
170 * @return string the zipped file
172 * @access public
174 function file()
176 $data = implode('', $this -> datasec);
177 $ctrldir = implode('', $this -> ctrl_dir);
179 return
180 $data .
181 $ctrldir .
182 $this -> eof_ctrl_dir .
183 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
184 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
185 pack('V', strlen($ctrldir)) . // size of central dir
186 pack('V', strlen($data)) . // offset to start of central dir
187 "\x00\x00"; // .zip file comment length
188 } // end of the 'file()' method
190 } // end of the 'zipfile' class