2.2.4
[phpmyadmin/arisferyanto.git] / libraries / zip.lib.php3
bloba8b40c9aa8ea99542e0fc1c0e8c90b7a1ee64a8e
1 <?php
2 /* $Id$ */
5 /**
6 * Zip file creation class.
7 * Makes zip files.
9 * Based on :
11 * http://www.zend.com/codex.php3?id=535&single=1
12 * By Eric Mueller (eric@themepark.com)
14 * http://www.zend.com/codex.php3?id=470&single=1
15 * by Denis125 (webmaster@atlant.ru)
17 * Official ZIP file format: http://www.pkware.com/appnote.txt
19 * @access public
21 class zipfile
23 /**
24 * Array to store compressed data
26 * @var array $datasec
28 var $datasec = array();
30 /**
31 * Central directory
33 * @var array $ctrl_dir
35 var $ctrl_dir = array();
37 /**
38 * End of central directory record
40 * @var string $eof_ctrl_dir
42 var $eof_ctrl_dir = "\x50\x4b\x05\x06\x00\x00\x00\x00";
44 /**
45 * Last offset position
47 * @var integer $old_offset
49 var $old_offset = 0;
52 /**
53 * Adds "file" to archive
55 * @param string file contents
56 * @param string name of the file in the archive (may contains the path)
58 * @access public
60 function addFile($data, $name)
62 $name = str_replace('\\', '/', $name);
64 $fr = "\x50\x4b\x03\x04";
65 $fr .= "\x14\x00"; // ver needed to extract
66 $fr .= "\x00\x00"; // gen purpose bit flag
67 $fr .= "\x08\x00"; // compression method
68 $fr .= "\x00\x00\x00\x00"; // last mod time and date
70 // "local file header" segment
71 $unc_len = strlen($data);
72 $crc = crc32($data);
73 $zdata = gzcompress($data);
74 $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
75 $c_len = strlen($zdata);
76 $fr .= pack('V', $crc); // crc32
77 $fr .= pack('V', $c_len); // compressed filesize
78 $fr .= pack('V', $unc_len); // uncompressed filesize
79 $fr .= pack('v', strlen($name)); // length of filename
80 $fr .= pack('v', 0); // extra field length
81 $fr .= $name;
83 // "file data" segment
84 $fr .= $zdata;
86 // "data descriptor" segment (optional but necessary if archive is not
87 // served as file)
88 $fr .= pack('V', $crc); // crc32
89 $fr .= pack('V', $c_len); // compressed filesize
90 $fr .= pack('V', $unc_len); // uncompressed filesize
92 // add this entry to array
93 $this -> datasec[] = $fr;
94 $new_offset = strlen(implode('', $this->datasec));
96 // now add to central directory record
97 $cdrec = "\x50\x4b\x01\x02";
98 $cdrec .= "\x00\x00"; // version made by
99 $cdrec .= "\x14\x00"; // version needed to extract
100 $cdrec .= "\x00\x00"; // gen purpose bit flag
101 $cdrec .= "\x08\x00"; // compression method
102 $cdrec .= "\x00\x00\x00\x00"; // last mod time & date
103 $cdrec .= pack('V', $crc); // crc32
104 $cdrec .= pack('V', $c_len); // compressed filesize
105 $cdrec .= pack('V', $unc_len); // uncompressed filesize
106 $cdrec .= pack('v', strlen($name) ); // length of filename
107 $cdrec .= pack('v', 0 ); // extra field length
108 $cdrec .= pack('v', 0 ); // file comment length
109 $cdrec .= pack('v', 0 ); // disk number start
110 $cdrec .= pack('v', 0 ); // internal file attributes
111 $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
113 $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header
114 $this -> old_offset = $new_offset;
116 $cdrec .= $name;
118 // optional extra field, file comment goes here
119 // save to central directory
120 $this -> ctrl_dir[] = $cdrec;
121 } // end of the 'addFile()' method
125 * Dumps out file
127 * @return string the zipped file
129 * @access public
131 function file()
133 $data = implode('', $this -> datasec);
134 $ctrldir = implode('', $this -> ctrl_dir);
136 return
137 $data .
138 $ctrldir .
139 $this -> eof_ctrl_dir .
140 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
141 pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
142 pack('V', strlen($ctrldir)) . // size of central dir
143 pack('V', strlen($data)) . // offset to start of central dir
144 "\x00\x00"; // .zip file comment length
145 } // end of the 'file()' method
147 } // end of the 'zipfile' class