Home > Backend Development > PHP Tutorial > PHP implements zip compression and decompression universal functions

PHP implements zip compression and decompression universal functions

WBOY
Release: 2016-07-25 08:45:18
Original
1175 people have browsed it
  1. function ezip($zip, $target = ''){
  2. $dirname=preg_replace('/.zip/', '', $zip);
  3. $root = $_SERVER['DOCUMENT_ROOT'].' /zip/';
  4. // echo $root. $zip;
  5. $zip = zip_open($root . $zip);
  6. // var_dump($zip);
  7. @mkdir($root . $target . $dirname.'/'.$zip_file);
  8. while($ zip_content = zip_read($zip)){
  9. $zip_file = zip_entry_name($zip_content);
  10. if(strpos($zip_file, '.')){
  11. $target_path = $root . $target . $dirname.'/'.$zip_file;
  12. @touch($destination_path);
  13. // echo $destination_path;
  14. $new_file = @fopen($destination_path, 'w+');
  15. @fwrite($new_file, zip_entry_read($ zip_content));
  16. @fclose($new_dosya);
  17. // $new_file;
  18. }else{
  19. @mkdir($root . $target . $dirname.'/'.$zip_file);
  20. // echo $root . $target . 'x/'.$zip_dosya;
  21. };
  22. };
  23. }
  24. // ezip('yuol.zip','./tr/');
  25. function zip($path) {
  26. $path=preg_replace( '//$/', '', $path);
  27. preg_match('//([dD][^/]*)$/', $path, $matches, PREG_OFFSET_CAPTURE);
  28. $filename=$matches[ 1][0].".zip";
  29. // var_dump($filename);
  30. // set_time_limit(0);
  31. $zip = new ZipArchive();
  32. $zip->open($filename,ZIPARCHIVE: :OVERWRITE);//return ;
  33. // var_dump($path);
  34. if (is_file($path)) {
  35. $path=preg_replace('////', '/', $path);
  36. $ base_dir=preg_replace('//[dD][^/]*$/', '/', $path);
  37. $base_dir=addcslashes($base_dir, '/:');
  38. $localname=preg_replace(' /'.$base_dir.'/', '', $path);
  39. // var_dump($localname);
  40. $zip->addFile($path,$localname);
  41. // var_dump($path);
  42. $zip->close();
  43. return;
  44. }elseif (is_dir($path)) {
  45. $path=preg_replace('//[dD][^/]*$/', '', $path );
  46. $base_dir=$path.'/';//基目录
  47. $base_dir=addcslashes($base_dir, '/:');
  48. // var_dump($base_dir);
  49. }
  50. $path=preg_replace(' ////', '/', $path);
  51. // var_dump($path);
  52. function addItem($path,&$zip,&$base_dir){
  53. // var_dump($path);
  54. $ handle = opendir($path);
  55. // var_dump($path);
  56. while (false !== ($file = readdir($handle))) {
  57. if (($file!='.')&&( $file!='..')){
  58. // var_dump($file);
  59. $ipath=$path.'/'.$file;
  60. if (is_file($ipath)){//条目是文件
  61. $localname=preg_replace('/'.$base_dir.'/', '', $ipath);
  62. var_dump($localname);
  63. $zip->addFile($ipath,$localname);
  64. // var_dump( $r);
  65. } else if (is_dir($ipath)){
  66. addItem($ipath,$zip,$base_dir);
  67. $localname=preg_replace('/'.$base_dir.'/', '', $ ipath);
  68. var_dump($localname);
  69. $zip->addEmptyDir($localname);
  70. }
  71. // var_dump($path);
  72. }
  73. }
  74. }
  75. // var_dump($base_dir);
  76. addItem ($path,$zip,$base_dir);
  77. $zip->close();
  78. }
  79. //调用方法
  80. zip('解压的目录');
复制代码

压缩解压, PHP, zip


source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template