示例#1
0
文件: zipfile.php 项目: rigidus/ea
 function _add($name, $file)
 {
     $handle = fopen($file, 'rb');
     $data = fread($handle, filesize($file));
     $name = str_replace("\\", "/", $name);
     //$name = str_replace("\\", "\\\\", $name);
     $fr = "PK";
     $fr .= "";
     // ver needed to extract
     $fr .= "";
     // gen purpose bit flag
     $fr .= "";
     // compression method
     $fr .= "";
     // last mod time and date
     $unc_len = strlen($data);
     $crc = crc32($data);
     $zdata = gzcompress($data);
     $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2);
     // fix crc bug
     $c_len = strlen($zdata);
     $fr .= pack("V", $crc);
     // crc32
     $fr .= pack("V", $c_len);
     //compressed filesize
     $fr .= pack("V", $unc_len);
     //uncompressed filesize
     $fr .= pack("v", strlen($name));
     //length of filename
     $fr .= pack("v", 0);
     //extra field length
     $fr .= $name;
     // end of "local file header" segment
     // "file data" segment
     $fr .= $zdata;
     // "data descriptor" segment (optional but necessary if archive is not served as file)
     $fr .= pack("V", $crc);
     //crc32
     $fr .= pack("V", $c_len);
     //compressed filesize
     $fr .= pack("V", $unc_len);
     //uncompressed filesize
     // add this entry to array
     self::$datasec[] = $fr;
     $new_offset = strlen(implode("", self::$datasec));
     // now add to central directory record
     $cdrec = "PK";
     $cdrec .= "";
     // version made by
     $cdrec .= "";
     // version needed to extract
     $cdrec .= "";
     // gen purpose bit flag
     $cdrec .= "";
     // compression method
     $cdrec .= "";
     // last mod time & date
     $cdrec .= pack("V", $crc);
     // crc32
     $cdrec .= pack("V", $c_len);
     //compressed filesize
     $cdrec .= pack("V", $unc_len);
     //uncompressed filesize
     $cdrec .= pack("v", strlen($name));
     //length of filename
     $cdrec .= pack("v", 0);
     //extra field length
     $cdrec .= pack("v", 0);
     //file comment length
     $cdrec .= pack("v", 0);
     //disk number start
     $cdrec .= pack("v", 0);
     //internal file attributes
     $cdrec .= pack("V", 32);
     //external file attributes - 'archive' bit set
     $cdrec .= pack("V", self::$old_offset);
     //relative offset of local header
     self::$old_offset = $new_offset;
     $cdrec .= $name;
     // optional extra field, file comment goes here
     // save to central directory
     self::$ctrl_dir[] = $cdrec;
 }