0

0

php ZIP压缩类实例分享

php中文网

php中文网

发布时间:2016-07-25 09:13:07

|

778人浏览过

|

来源于php中文网

原创

功能:将文件压缩成zip,或者rar的压缩包。后缀名可以自定义。 使用方法:首先实例化,然后传参。两个参数。 第一个关于文件地址的一个Array。 第二个是要保存的压缩包文件的绝对地址。

php zip压缩类的调用方法:

  1. $zipfiles =array("/root/pooy/test1.txt","/root/pooy/test2.txt");
  2. $z = new PHPZip();
  3. //$randomstr = random(8);
  4. $zipfile = TEMP."/photocome_".$groupid.".zip";
  5. $z->Zip($zipfiles, $zipfile); //添加文件列表
复制代码

PHP的ZIP压缩类:

  1. #
  2. # phpzip v1.2 by sext (sext@neud.net) 2002-11-18
  3. # (changed: 2003-03-01)
  4. #
  5. # makes zip archive
  6. #
  7. # based on "zip file creation class", uses zlib
  8. #
  9. #
  10. class phpzip
  11. {
  12. function zip($dir, $zipfilename)
  13. {
  14. if (@function_exists('gzcompress'))
  15. {
  16. $curdir = getcwd();
  17. if (is_array($dir))
  18. {
  19. $filelist = $dir;
  20. }
  21. else
  22. {
  23. $filelist = $this -> getfilelist($dir);
  24. }
  25. if ((!empty($dir))&&(!is_array($dir))&&(file_exists($dir))) chdir($dir);

  26. else chdir($curdir);
  27. if (count($filelist)>0)

  28. {
  29. foreach($filelist as $filename)
  30. {
  31. if (is_file($filename))
  32. {
  33. $fd = fopen ($filename, "r");
  34. $content = fread ($fd, filesize ($filename));
  35. fclose ($fd);
  36. if (is_array($dir)) $filename = basename($filename);

  37. $this -> addFile($content, $filename);
  38. }
  39. }
  40. $out = $this -> file();
  41. chdir($curdir);

  42. $fp = fopen($zipfilename, "w");
  43. fwrite($fp, $out, strlen($out));
  44. fclose($fp);
  45. }
  46. return 1;
  47. }
  48. else return 0;
  49. }

    立即学习PHP免费学习笔记(深入)”;

  50. function GetFileList($dir)

  51. {
  52. if (file_exists($dir))
  53. {
  54. $args = func_get_args();
  55. $pref = $args[1];
  56. $dh = opendir($dir);

  57. while($files = readdir($dh))
  58. {
  59. if (($files!=".")&&($files!=".."))
  60. {
  61. if (is_dir($dir.$files))
  62. {
  63. $curdir = getcwd();
  64. chdir($dir.$files);
  65. $file = array_merge($file, $this -> GetFileList("", "$pref$files/"));
  66. chdir($curdir);
  67. }
  68. else $file[]=$pref.$files;
  69. }
  70. }
  71. closedir($dh);
  72. }
  73. return $file;
  74. }
  75. var $datasec = array();

  76. var $ctrl_dir = array();
  77. var $eof_ctrl_dir = "PK";
  78. var $old_offset = 0;
  79. /**

  80. * Converts an Unix timestamp to a four byte DOS date and time format (date
  81. * in high two bytes, time in low two bytes allowing magnitude comparison).
  82. *
  83. * @param integer the current Unix timestamp
  84. *
  85. * @return integer the current date in a four byte DOS format
  86. *
  87. * @access private
  88. */
  89. function unix2DosTime($unixtime = 0) {
  90. $timearray = ($unixtime == 0) ? getdate() : getdate($unixtime);
  91. if ($timearray['year']

  92. $timearray['year'] = 1980;
  93. $timearray['mon'] = 1;
  94. $timearray['mday'] = 1;
  95. $timearray['hours'] = 0;
  96. $timearray['minutes'] = 0;
  97. $timearray['seconds'] = 0;
  98. } // end if
  99. return (($timearray['year'] - 1980)

  100. ($timearray['hours'] > 1);
  101. } // end of the 'unix2DosTime()' method
  102. /**

  103. * Adds "file" to archive
  104. *
  105. * @param string file contents
  106. * @param string name of the file in the archive (may contains the path)
  107. * @param integer the current timestamp
  108. *
  109. * @access public
  110. */
  111. function addFile($data, $name, $time = 0)
  112. {
  113. $name = str_replace('\', '/', $name);
  114. $dtime = dechex($this->unix2DosTime($time));

  115. $hexdtime = 'x' . $dtime[6] . $dtime[7]
  116. Western union point . 'x' . $dtime[4] . $dtime[5]
  117. . 'x' . $dtime[2] . $dtime[3]
  118. . 'x' . $dtime[0] . $dtime[1];
  119. eval('$hexdtime = "' . $hexdtime . '";');
  120. $fr = "PK";

    轻松商城
    轻松商城

    打造属于自己的全新网上购物商城,尽在“轻松商城”商务平台 订单邮件,html页面生成,后台登陆检测,无限级商品分类,商品分类共享,辅之以在线客服客服手段,将你的订单即时跟踪,提高客户购物的容易度。自动成 比例压缩生成JPG/GIF图片,多种网上支付端口,在线编辑简介网页,自定义商品属性,自定义商品简介魔板,自由增加一级页面数量,自由更换页面样式, 自由设

    下载
  121. $fr .= ""; // ver needed to extract
  122. $fr .= ""; // gen purpose bit flag
  123. $fr .= ""; // compression method
  124. $fr .= $hexdtime; // last mod time and date
  125. // "local file header" segment

  126. $unc_len = strlen($data);
  127. $crc = crc32($data);
  128. $zdata = gzcompress($data);
  129. $c_len = strlen($zdata);
  130. $zdata = substr(substr($zdata, 0, strlen($zdata) - 4), 2); // fix crc bug
  131. $fr .= pack('V', $crc); // crc32
  132. $fr .= pack('V', $c_len); // compressed filesize
  133. $fr .= pack('V', $unc_len); // uncompressed filesize
  134. $fr .= pack('v', strlen($name)); // length of filename
  135. $fr .= pack('v', 0); // extra field length
  136. $fr .= $name;
  137. // "file data" segment

  138. $fr .= $zdata;
  139. // "data descriptor" segment (optional but necessary if archive is not

  140. // served as file)
  141. $fr .= pack('V', $crc); // crc32
  142. $fr .= pack('V', $c_len); // compressed filesize
  143. $fr .= pack('V', $unc_len); // uncompressed filesize
  144. // add this entry to array

  145. $this -> datasec[] = $fr;
  146. $new_offset = strlen(implode('', $this->datasec));
  147. // now add to central directory record

  148. $cdrec = "PK";
  149. $cdrec .= ""; // version made by
  150. $cdrec .= ""; // version needed to extract
  151. $cdrec .= ""; // gen purpose bit flag
  152. $cdrec .= ""; // compression method
  153. $cdrec .= $hexdtime; // last mod time & date
  154. $cdrec .= pack('V', $crc); // crc32
  155. $cdrec .= pack('V', $c_len); // compressed filesize
  156. $cdrec .= pack('V', $unc_len); // uncompressed filesize
  157. $cdrec .= pack('v', strlen($name) ); // length of filename
  158. $cdrec .= pack('v', 0 ); // extra field length
  159. $cdrec .= pack('v', 0 ); // file comment length
  160. $cdrec .= pack('v', 0 ); // disk number start
  161. $cdrec .= pack('v', 0 ); // internal file attributes
  162. $cdrec .= pack('V', 32 ); // external file attributes - 'archive' bit set
  163. $cdrec .= pack('V', $this -> old_offset ); // relative offset of local header

  164. $this -> old_offset = $new_offset;
  165. $cdrec .= $name;

  166. // optional extra field, file comment goes here

  167. // save to central directory
  168. $this -> ctrl_dir[] = $cdrec;
  169. } // end of the 'addFile()' method
  170. /**

  171. * Dumps out file
  172. *
  173. * @return string the zipped file
  174. *
  175. * @access public
  176. */
  177. function file()
  178. {
  179. $data = implode('', $this -> datasec);
  180. $ctrldir = implode('', $this -> ctrl_dir);
  181. return

  182. $data .
  183. $ctrldir .
  184. $this -> eof_ctrl_dir .
  185. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries "on this disk"
  186. pack('v', sizeof($this -> ctrl_dir)) . // total # of entries overall
  187. pack('V', strlen($ctrldir)) . // size of central dir
  188. pack('V', strlen($data)) . // offset to start of central dir
  189. ""; // .zip file comment length
  190. } // end of the 'file()' method
  191. } // end of the 'PHPZip' class

  192. ?>
复制代码

老外实现的php zip压缩类,个人感觉还是不错的,里面有大量php实用函数的应用技巧,特别是函数pack、crc32、gzcompress等函数的应用,值得学习借鉴。



相关文章

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载

本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法
pixiv网页版官网登录与阅读指南_pixiv官网直达入口与在线访问方法

本专题系统整理pixiv网页版官网入口及登录访问方式,涵盖官网登录页面直达路径、在线阅读入口及快速进入方法说明,帮助用户高效找到pixiv官方网站,实现便捷、安全的网页端浏览与账号登录体验。

1142

2026.02.13

微博网页版主页入口与登录指南_官方网页端快速访问方法
微博网页版主页入口与登录指南_官方网页端快速访问方法

本专题系统整理微博网页版官方入口及网页端登录方式,涵盖首页直达地址、账号登录流程与常见访问问题说明,帮助用户快速找到微博官网主页,实现便捷、安全的网页端登录与内容浏览体验。

371

2026.02.13

Flutter跨平台开发与状态管理实战
Flutter跨平台开发与状态管理实战

本专题围绕Flutter框架展开,系统讲解跨平台UI构建原理与状态管理方案。内容涵盖Widget生命周期、路由管理、Provider与Bloc状态管理模式、网络请求封装及性能优化技巧。通过实战项目演示,帮助开发者构建流畅、可维护的跨平台移动应用。

245

2026.02.13

TypeScript工程化开发与Vite构建优化实践
TypeScript工程化开发与Vite构建优化实践

本专题面向前端开发者,深入讲解 TypeScript 类型系统与大型项目结构设计方法,并结合 Vite 构建工具优化前端工程化流程。内容包括模块化设计、类型声明管理、代码分割、热更新原理以及构建性能调优。通过完整项目示例,帮助开发者提升代码可维护性与开发效率。

37

2026.02.13

Redis高可用架构与分布式缓存实战
Redis高可用架构与分布式缓存实战

本专题围绕 Redis 在高并发系统中的应用展开,系统讲解主从复制、哨兵机制、Cluster 集群模式及数据分片原理。内容涵盖缓存穿透与雪崩解决方案、分布式锁实现、热点数据优化及持久化策略。通过真实业务场景演示,帮助开发者构建高可用、可扩展的分布式缓存系统。

114

2026.02.13

c语言 数据类型
c语言 数据类型

本专题整合了c语言数据类型相关内容,阅读专题下面的文章了解更多详细内容。

77

2026.02.12

雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法
雨课堂网页版登录入口与使用指南_官方在线教学平台访问方法

本专题系统整理雨课堂网页版官方入口及在线登录方式,涵盖账号登录流程、官方直连入口及平台访问方法说明,帮助师生用户快速进入雨课堂在线教学平台,实现便捷、高效的课程学习与教学管理体验。

17

2026.02.12

豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法
豆包AI网页版入口与智能创作指南_官方在线写作与图片生成使用方法

本专题汇总豆包AI官方网页版入口及在线使用方式,涵盖智能写作工具、图片生成体验入口和官网登录方法,帮助用户快速直达豆包AI平台,高效完成文本创作与AI生图任务,实现便捷智能创作体验。

863

2026.02.12

PostgreSQL性能优化与索引调优实战
PostgreSQL性能优化与索引调优实战

本专题面向后端开发与数据库工程师,深入讲解 PostgreSQL 查询优化原理与索引机制。内容包括执行计划分析、常见索引类型对比、慢查询优化策略、事务隔离级别以及高并发场景下的性能调优技巧。通过实战案例解析,帮助开发者提升数据库响应速度与系统稳定性。

123

2026.02.12

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

相关下载

更多

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号