PHP Implementation zip Compression decompression General function

Source: Internet
Author: User
Tags ziparchive
  1. function Ezip ($zip, $hedef = ") {
  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. $hedef. $dirname. ' /'. $zip _dosya);
  8. while ($zip _icerik = Zip_read ($zip)) {
  9. $zip _dosya = zip_entry_name ($zip _icerik);
  10. if (Strpos ($zip _dosya, '. ')) {
  11. $hedef _yol = $root. $hedef. $dirname. ' /'. $zip _dosya;
  12. @touch ($hedef _yol);
  13. echo $hedef _yol;
  14. $yeni _dosya = @fopen ($hedef _yol, ' w+ ');
  15. @fwrite ($yeni _dosya, Zip_entry_read ($zip _icerik));
  16. @fclose ($yeni _dosya);
  17. $yeni _dosya;
  18. }else{
  19. @mkdir ($root. $hedef. $dirname. ' /'. $zip _dosya);
  20. Echo $root. $hedef. ' x/'. $zip _dosya;
  21. };
  22. };
  23. }
  24. Ezip (' Yuol.zip ', './tr/');
  25. function Zip ($path) {
  26. $path =preg_replace ('/\/$/', ', $path);
  27. Preg_match ('/\/([\d\d][^\/]*) $/', $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 ('/\/[\d\d][^\/]*$/', '/', $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 ('/\/[\d\d][^\/]*$/', ', $path);
  46. $base _dir= $path. ' /';//Base directory
  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)) {//entry is a file
  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. Calling methods
  80. Zip (' unzipped directory ');
Copy Code
Compression decompression, PHP, zip
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.