您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

一个php把一组文件打包成zip的类

2025/3/6 14:05:20发布24次查看
这段php类可以挨个添加文件到数组,最后将添加的文件打包成zip
/* $id: zip.lib.php,v 1.1 2004/02/14 15:21:18 anoncvs_tusedb exp $ */// vim: expandtab sw=4 ts=4 sts=4: /*** zip file creation class.* makes zip files.** last modification and extension by :** hasin hayder* homepage : www.hasinme.info* email : countdraculla@gmail.com* ide : php designer 2005*** originally based on :** http://www.zend.com/codex.php?id=535&single=1* by eric mueller ** http://www.zend.com/codex.php?id=470&single=1* by denis125 ** a patch from peter listiak for last modified* date and time of the compressed file** official zip file format: http://www.pkware.com/appnote.txt** @access public*/class zipfile{ /** * array to store compressed data * * @public array $datasec */ public $datasec = array(); /** * central directory * * @public array $ctrl_dir */ public $ctrl_dir = array(); /** * end of central directory record * * @public string $eof_ctrl_dir */ public $eof_ctrl_dir = \x50\x4b\x05\x06\x00\x00\x00\x00; /** * last offset position * * @public integer $old_offset */ public $old_offset = 0; /** * converts an unix timestamp to a four byte dos date and time format (date * in high two bytes, time in low two bytes allowing magnitude comparison). * * @param integer the current unix timestamp * * @return integer the current date in a four byte dos format * * @access private */ function unix2dostime($unixtime = 0) { $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime); if ($timearray['year'] $timearray['year'] = 1980; $timearray['mon'] = 1; $timearray['mday'] = 1; $timearray['hours'] = 0; $timearray['minutes'] = 0; $timearray['seconds'] = 0; } // end if return (($timearray['year'] - 1980) ($timearray['hours'] > 1); } // end of the 'unix2dostime()' method /** * adds file to archive * * @param string file contents * @param string name of the file in the archive (may contains the path) * @param integer the current timestamp * * @access public */ function addfile($data, $name, $time = 0) { $name = str_replace('\\', '/', $name); $dtime = dechex($this->unix2dostime($time)); $hexdtime = '\x' . $dtime[6] . $dtime[7] . '\x' . $dtime[4] . $dtime[5] . '\x' . $dtime[2] . $dtime[3] . '\x' . $dtime[0] . $dtime[1]; eval('$hexdtime = ' . $hexdtime . ';'); $fr = \x50\x4b\x03\x04; $fr .= \x14\x00; // ver needed to extract $fr .= \x00\x00; // gen purpose bit flag $fr .= \x08\x00; // compression method $fr .= $hexdtime; // last mod time and date // local file header segment $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; // 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 $this -> datasec[] = $fr; // now add to central directory record $cdrec = \x50\x4b\x01\x02; $cdrec .= \x00\x00; // version made by $cdrec .= \x14\x00; // version needed to extract $cdrec .= \x00\x00; // gen purpose bit flag $cdrec .= \x08\x00; // compression method $cdrec .= $hexdtime; // 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', $this -> old_offset ); // relative offset of local header $this -> old_offset += strlen($fr); $cdrec .= $name; // optional extra field, file comment goes here // save to central directory $this -> ctrl_dir[] = $cdrec; } // end of the 'addfile()' method /** * dumps out file * * @return string the zipped file * * @access public */ function file() { $data = implode('', $this -> datasec); $ctrldir = implode('', $this -> ctrl_dir); return $data . $ctrldir . $this -> eof_ctrl_dir . pack('v', sizeof($this -> ctrl_dir)) . // total # of entries on this disk pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall pack('v', strlen($ctrldir)) . // size of central dir pack('v', strlen($data)) . // offset to start of central dir \x00\x00; // .zip file comment length } // end of the 'file()' method /** * a wrapper of original addfile function * * created by hasin hayder at 29th jan, 1:29 am * * @param array an array of files with relative/absolute path to be added in zip file * * @access public */ function addfiles($files /*only pass array*/) { foreach($files as $file) { if (is_file($file)) //directory check { $data = implode(,file($file)); $this->addfile($data,$file); } } } /** * a wrapper of original file function * * created by hasin hayder at 29th jan, 1:29 am * * @param string output file name * * @access public */ function output($file) { $fp=fopen($file,w); fwrite($fp,$this->file()); fclose($fp); } } // end of the 'zipfile' class
复制代码
php, zip
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product