zipChajian.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. class zipChajian extends Chajian{
  3. /**
  4. * 解压zip文件
  5. */
  6. public function unzip($filename, $path){
  7. if(!function_exists('zip_open'))return 'php未开启zip模块';
  8. if(!file_exists($filename))return '文件不存在';
  9. @$resource = zip_open($filename);
  10. if(!$resource)return '无法打开文件';
  11. while ($dir_resource = zip_read($resource)){
  12. if(zip_entry_open($resource,$dir_resource)){
  13. $file_name = $path.zip_entry_name($dir_resource);
  14. $file_path = substr($file_name,0,strrpos($file_name, "/"));
  15. if(!is_dir($file_path))mkdir($file_path,0777,true);
  16. if(!is_dir($file_name)){
  17. $file_size = zip_entry_filesize($dir_resource);
  18. $file_content = zip_entry_read($dir_resource, $file_size);
  19. $bos = $this->writefile($file_name, $file_content);
  20. if(!$bos)return '无权限写入文件:'.$file_name.'';
  21. }
  22. zip_entry_close($dir_resource);
  23. }
  24. }
  25. zip_close($resource);
  26. return 'ok';
  27. }
  28. private function writefile($file_name, $file_content)
  29. {
  30. $oldcont = '';
  31. if(file_exists($file_name)){
  32. $oldcont = file_get_contents($file_name);
  33. if($oldcont != $file_content){
  34. $barfile = ''.UPDIR.'/upage/'.$file_name.'';
  35. $this->rock->createdir($barfile);
  36. copy($file_name, $barfile);
  37. }
  38. }
  39. @$bos = file_put_contents($file_name,$file_content);
  40. return $bos;
  41. }
  42. /**
  43. * 获取zip上文件
  44. */
  45. public function zipget($filename){
  46. if(!function_exists('zip_open'))return 'php未开启zip模块';
  47. if(!file_exists($filename))return '文件不存在';
  48. @$resource = zip_open($filename);
  49. if(!$resource)return '无法打开文件';
  50. $farr = array();
  51. while ($dir_resource = zip_read($resource)){
  52. if(zip_entry_open($resource,$dir_resource)){
  53. $file_name = zip_entry_name($dir_resource);
  54. $file_path = substr($file_name,0,strrpos($file_name, "/"));
  55. if(!is_dir($file_name) && substr($file_name,-1)!='/'){
  56. $file_size = zip_entry_filesize($dir_resource);
  57. $file_content = zip_entry_read($dir_resource, $file_size);
  58. if(substr($file_name,0,1)=='/')$file_name = substr($file_name,1);
  59. $farr[] = array(
  60. 'filepath' => $file_name,
  61. 'filesize' => $file_size,
  62. 'filecontent' => base64_encode($file_content),
  63. );
  64. }
  65. zip_entry_close($dir_resource);
  66. }
  67. }
  68. zip_close($resource);
  69. return $farr;
  70. }
  71. private function addFileToZip($path, $zip, $wz){
  72. $handler = opendir($path);
  73. while(($filename=readdir($handler))!==false){
  74. if($filename != '.' && $filename != '..'){
  75. $addfile = $path.'/'.$filename;
  76. if(is_dir($addfile)){
  77. $this->addFileToZip($addfile, $zip, $wz);
  78. }else{
  79. $localwz = substr($addfile, $wz);
  80. $zip->addFile($addfile, $localwz);
  81. }
  82. }
  83. }
  84. @closedir($path);
  85. }
  86. /**
  87. * zip打包
  88. */
  89. public function packzip($path, $topath)
  90. {
  91. $zip = new ZipArchive();
  92. if($zip->open($topath, ZipArchive::OVERWRITE)=== TRUE){
  93. $this->addFileToZip($path, $zip, strlen($path));
  94. $zip->close();
  95. }
  96. if(!file_exists($topath))$topath='';
  97. return $topath;
  98. }
  99. }
粤ICP备19079148号