6 * Zip file creation class.
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
24 * Array to store compressed data
28 var $datasec = array();
33 * @var array $ctrl_dir
35 var $ctrl_dir = array();
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";
45 * Last offset position
47 * @var integer $old_offset
53 * Adds "file" to archive
55 * @param string file contents
56 * @param string name of the file in the archive (may contains the path)
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);
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
83 // "file data" segment
86 // "data descriptor" segment (optional but necessary if archive is not
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;
118 // optional extra field, file comment goes here
119 // save to central directory
120 $this -> ctrl_dir
[] = $cdrec;
121 } // end of the 'addFile()' method
127 * @return string the zipped file
133 $data = implode('', $this -> datasec
);
134 $ctrldir = implode('', $this -> ctrl_dir
);
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