PHP Class: Unzip files


/ Published in: PHP
Save to your folder(s)

Unzip files with PHP.


Copy this code and paste it in your HTML
  1. <?php
  2. /**************************************
  3.  seesaw associates | http://seesaw.net
  4.  
  5.  client:
  6.  file:
  7.  description:
  8.  
  9.  Copyright (C) 2008 Matt Kenefick(.com)
  10. **************************************/
  11.  
  12. class zipArch {
  13. /**
  14. * Unzip the source_file in the destination dir
  15. *
  16. * @param string The path to the ZIP-file.
  17. * @param string The path where the zipfile should be unpacked, if false the directory of the zip-file is used
  18. * @param boolean Indicates if the files will be unpacked in a directory with the name of the zip-file (true) or not (false) (only if the destination directory is set to false!)
  19. * @param boolean Overwrite existing files (true) or not (false)
  20. *
  21. * @return boolean Succesful or not
  22. */
  23. function unzip($src_file, $dest_dir=false, $create_zip_name_dir=true, $overwrite=true)
  24. {
  25. if(function_exists("zip_open"))
  26. {
  27. if(!is_resource(zip_open($src_file)))
  28. {
  29. $src_file=dirname($_SERVER['SCRIPT_FILENAME'])."/".$src_file;
  30. }
  31.  
  32. if (is_resource($zip = zip_open($src_file)))
  33. {
  34. $splitter = ($create_zip_name_dir === true) ? "." : "/";
  35. if ($dest_dir === false) $dest_dir = substr($src_file, 0, strrpos($src_file, $splitter))."/";
  36.  
  37. // Create the directories to the destination dir if they don't already exist
  38. $this->create_dirs($dest_dir);
  39.  
  40. // For every file in the zip-packet
  41. while ($zip_entry = zip_read($zip))
  42. {
  43. // Now we're going to create the directories in the destination directories
  44.  
  45. // If the file is not in the root dir
  46. $pos_last_slash = strrpos(zip_entry_name($zip_entry), "/");
  47. if ($pos_last_slash !== false)
  48. {
  49. // Create the directory where the zip-entry should be saved (with a "/" at the end)
  50. $this->create_dirs($dest_dir.substr(zip_entry_name($zip_entry), 0, $pos_last_slash+1));
  51. }
  52.  
  53. // Open the entry
  54. if (zip_entry_open($zip,$zip_entry,"r"))
  55. {
  56.  
  57. // The name of the file to save on the disk
  58. $file_name = $dest_dir.zip_entry_name($zip_entry);
  59.  
  60. // Check if the files should be overwritten or not
  61. if ($overwrite === true || $overwrite === false && !is_file($file_name))
  62. {
  63. // Get the content of the zip entry
  64. $fstream = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry));
  65.  
  66. if(!is_dir($file_name))
  67. file_put_contents($file_name, $fstream );
  68. // Set the rights
  69. if(file_exists($file_name))
  70. {
  71. chmod($file_name, 0777);
  72. echo "<span style=\"color:#1da319;\">file saved: </span>".$file_name."<br />";
  73. }
  74. else
  75. {
  76. echo "<span style=\"color:red;\">file not found: </span>".$file_name."<br />";
  77. }
  78. }
  79.  
  80. // Close the entry
  81. zip_entry_close($zip_entry);
  82. }
  83. }
  84. // Close the zip-file
  85. zip_close($zip);
  86. }
  87. else
  88. {
  89. echo "No Zip Archive Found.";
  90. return false;
  91. }
  92.  
  93. return true;
  94. }
  95. else
  96. {
  97. if(version_compare(phpversion(), "5.2.0", "<"))
  98. $infoVersion="(use PHP 5.2.0 or later)";
  99.  
  100. echo "You need to install/enable the php_zip.dll extension $infoVersion";
  101. }
  102. }
  103.  
  104. function create_dirs($path)
  105. {
  106. if (!is_dir($path))
  107. {
  108. $directory_path = "";
  109. $directories = explode("/",$path);
  110. array_pop($directories);
  111.  
  112. foreach($directories as $directory)
  113. {
  114. $directory_path .= $directory."/";
  115. if (!is_dir($directory_path))
  116. {
  117. mkdir($directory_path);
  118. chmod($directory_path, 0777);
  119. }
  120. }
  121. }
  122. }
  123.  
  124. }
  125. ?>

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.