




已阅读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. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 购物中心餐饮区特色餐饮品牌入驻合同
- 经济学的政策工具试题及答案
- 思政课教学改革新路径探索
- 2025年生物制药中试车间建设产业生态与产业链协同发展报告
- 公共关系学的前沿知识试题及答案
- 2025年行政管理学考试全景分析及试题及答案
- 项目财务管理实务试题及答案
- 2024水利水电工程综合能力考评试题及答案
- 工业互联网平台可信执行环境(TEE)在智能工厂设备故障预防中的应用报告
- 2025年中级经济师考试的复习资源开源与试题及答案
- 2024年四川绵阳科技城新区招聘社区工作者考试真题
- 2025年北师大版七年级数学下册计算题专项训练专题03与乘法公式有关的计算(三大题型总结)(原卷版+解析)
- 新时代幼儿园教师职业行为十项准则培训
- 2025-2030中国甘蔗收割机行业市场现状供需分析及投资评估规划分析研究报告
- 2024年青岛市市南区教育系统招聘教师考试真题
- 人工智能背景下的高职艺术设计专业教学模式探索
- 波罗的海BDI指数历史行情(1985年05月16日-2025年3月31日)
- 2025年江苏南通苏北七市高三二模语文作文分析
- 2025届百师联盟高三联考模拟预测(冲刺二)语文试题含答案
- 2024年行政执法证考试必刷题库及答案(共432题)
- 外研版(三起)(2024)三年级下册英语Unit 2 单元测试卷(含答案)
评论
0/150
提交评论