首页 > 开发 > PHP > 正文

php生成zip文件类实例

2024-05-04 23:34:00
字体:大 中 小
来源:转载
供稿:网友

这篇文章主要介绍了php生成zip文件类,实例分析了php操作zip文件的技巧,非常具有实用价值,需要的朋友可以参考下

本文实例讲述了php生成zip文件类。分享给大家供大家参考。具体如下:

 

 
  1. <?php 
  2. /* 
  3. By: Matt Ford 
  4. Purpose: Basic class to create zipfiles 
  5. */ 
  6. class zipFile { 
  7. public $files = array(); 
  8. public $settings = NULL; 
  9. public $fileInfo = array ( 
  10. "name" => "", 
  11. "numFiles" => 0, 
  12. "fullFilePath" => "" 
  13. ); 
  14. private $fileHash = ""; 
  15. private $zip = ""; 
  16. public function __construct($settings) { 
  17. $this->zipFile($settings); 
  18. } 
  19. public function zipFile($settings) { 
  20. $this->zip = new ZipArchive(); 
  21. $this->settings = new stdClass(); 
  22. foreach ($settings as $k => $v) { 
  23. $this->settings->$k = $v; 
  24. } 
  25. } 
  26. public function create() { 
  27. $this->fileHash = md5(implode(",", $this->files)); 
  28. $this->fileInfo["name"] = $this->fileHash . ".zip"; 
  29. $this->fileInfo["numFiles"] = count($this->files); 
  30. $this->fileInfo["fullFilePath"] = $this->settings->path .  
  31. "/" . $this->fileInfo["name"]; 
  32. if (file_exists($this->fileInfo["fullFilePath"])) { 
  33. return array ( 
  34. false, 
  35. "already created: " . $this->fileInfo["fullFilePath"] 
  36. ); 
  37. } 
  38. else { 
  39. $this->zip->open($this->fileInfo["fullFilePath"], ZIPARCHIVE::CREATE); 
  40. $this->addFiles(); 
  41. $this->zip->close(); 
  42. return array ( 
  43. true, 
  44. "new file created: " . $this->fileInfo["fullFilePath"] 
  45. ); 
  46. } 
  47. } 
  48. private function addFiles() { 
  49. foreach ($this->files as $k) { 
  50. $this->zip->addFile($k, basename($k)); 
  51. } 
  52. } 
  53. } 
  54. $settings = array ( 
  55. "path" => dirname(__FILE__) 
  56. ); 
  57. $zipFile = new zipFile($settings); 
  58. $zipFile->files = array ( 
  59. "./images/navoff.jpg", 
  60. "./images/navon.jpg" 
  61. ); 
  62. list($success, $error) = $zipFile->create(); 
  63. if ($success === true) { 
  64. //success 
  65. } 
  66. else { 
  67. //error because: $error 
  68. } 
  69. ?> 

希望本文所述对大家的php程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表