




已阅读5页,还剩4页未读, 继续免费阅读
版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
一个完美的PHP ZIP压缩类代码本程序可以快速的实现把我们的文件利用php压缩类压缩成我们想的zip,或者rar 的压缩包,后缀名可以自定义哦, 压缩算法是来自国外一个网站抄的。首先实例化,然后传参。两个参数。第一个关于你文件地址的一个Array。第二个是要你要保存的压缩包文件的绝对地址。For example:代码如下复制代码 $zipfiles =array(/root/pooy/test1.txt,/root/pooy/test2.txt); $z = new PHPZip(); /$randomstr = random(8); $zipfile = TEMP./photocome_.$groupid.zip; $z-Zip($zipfiles, $zipfile);/添加文件列表PHP的ZIP压缩类如下:代码如下复制代码 GetFileList($dir); if (!empty($dir)&(!is_array($dir)&(file_exists($dir) chdir($dir); else chdir($curdir); if (count($filelist)0) foreach($filelist as $filename) if (is_file($filename) $fd =fopen($filename, r); $content = fread ($fd, filesize ($filename); fclose ($fd); if (is_array($dir) $filename = basename($filename); $this - addFile($content, $filename); $out = $this - file(); chdir($curdir); $fp = fopen($zipfilename, w); fwrite($fp, $out, strlen($out); fclose($fp); return 1; else return 0; function GetFileList($dir) if (file_exists($dir) $args = func_get_args(); $pref = $args1; $dh =opendir($dir); while($files = readdir($dh) if ($files!=.)&($files!=.) if (is_dir($dir.$files) $curdir = getcwd(); chdir($dir.$files); $file = array_merge($file, $this - GetFileList(, $pref$files/); chdir($curdir); else $file=$pref.$files; closedir($dh); return $file; var $datasec = array(); var $ctrl_dir = array(); var $eof_ctrl_dir = x50x4bx05x06x00x00x00x00; var $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 ing 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 ($timearrayyear 1980) $timearrayyear = 1980; $timearraymon = 1; $timearraymday = 1; $timearrayhours = 0; $timearrayminutes = 0; $timearrayseconds = 0; / end if return ($timearrayyear - 1980) 25) | ($timearraymon 21) | ($timearraymday 16) | ($timearrayhours 11) | ($timearrayminutes 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 . $dtime6 . $dtime7 . x . $dtime4 . $dtime5 . x . $dtime2 . $dtime3 . x . $dtime0 . $dtime1; eval($hexdtime = . $hexdtime . ;); $fr = x50x4bx03x04; $fr .= x14x00; / ver needed to extract $fr .= x00x00; / gen purpose bit flag $fr .= x08x00; / compression method $fr .= $hexdtime; / last mod time and date / local file header segment $unc_len = strlen($data); $crc = crc32($data); $zdata = gzcompress($data); $c_len = strlen($zdata); $zdata =substr(substr($zdata, 0, strlen($zdata) - 4), 2); / fix crc bug $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; $new_offset = strlen(implode(, $this-datasec); / now add to central directory record $cdrec = x50x4bx01x02; $cdrec .= x00x00; / version made by $cdrec .= x14x00; / version needed to extract $cdrec .= x00x00; / gen purpose bit flag $cdrec .= x08x00; / 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 = $new_offset; $cdrec .= $name; / optional extra field, file comment goes here / save to central directory $this - ctrl_dir = $cdrec; / end of the addFile() method /* * Dumpsout 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, si
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 环境保护与绿色发展对县域经济空间格局的挑战与机遇
- 高附加值产品创新对羊绒产业链升级的驱动
- 从教育政策看办公行业的未来趋势
- 大学生创业创新教育与跨境电商企业合作模式探讨
- 江苏省南京市建邺三校联合2024年化学九上期末统考模拟试题含解析
- 云南省曲靖市2025届八年级物理第一学期期末考试试题含解析
- 山东省张店区七校联考2025届化学九上期末调研试题含解析
- 2025至2030沉香木行业运营态势与投资前景调查研究报告
- 2025至2030中国自动报警系统(AWS)行业市场占有率及投资前景评估规划报告
- 2025至2030中国自动升降机行业发展分析及前景趋势与发展趋势分析与未来投资战略咨询研究报告
- 毁林毁草违规行为集中整治实施方案
- 日本2025年食品过敏原培训
- 中新天津生态城教育系统招聘教职人员笔试真题2024
- GB/T 45817-2025消费品质量分级陶瓷砖
- DB37∕T 5302-2024 既有城市桥梁现场检测及承载能力评估技术标准
- 安徽省池州市贵池区2024-2025学年八年级下学期数学期末检测试卷(含答案)
- 正畸接诊标准化流程
- 中心静脉压(CVP)监测与护理
- 酒店前台接待礼仪与沟通技巧练习题
- 床上用品采购 投标方案
- 口腔工艺管理课件
评论
0/150
提交评论