Source for file Dearchiver.class.php

Documentation is available at Dearchiver.class.php

  1. <?php
  2. /**
  3. * @since 06/10/05
  4. * @package harmoni.utilities
  5. *
  6. * @copyright Copyright &copy; 2005, Middlebury College
  7. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  8. *
  9. * @version $Id: Dearchiver.class.php,v 1.6 2006/06/26 12:55:14 adamfranco Exp $
  10. *
  11. * @link http://sourceforge.net/projects/concerto
  12. */
  13.  
  14. /**
  15. * Uncompresses and dearchives files from the input file.
  16. *
  17. * @package harmoni.utilities
  18. *
  19. * @copyright Copyright &copy; 2005, Middlebury College
  20. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License (GPL)
  21. *
  22. * @version $Id: Dearchiver.class.php,v 1.6 2006/06/26 12:55:14 adamfranco Exp $
  23. */
  24.  
  25. require_once("Archive/Tar.php");
  26. require_once(HARMONI."utilities/mkdirr.function.php");
  27.  
  28. class Dearchiver {
  29. /**
  30. * Figures out the type of archive.
  31. *
  32. * @param string filename
  33. * @return string the extension of the file (zip, gz, bz2, tar.gz, tar.bz2)
  34. * @access private
  35. * @since 06/10/05
  36. *
  37. */
  38. function _getFileType($filename) {
  39. $filenameParts = explode(".", $filename);
  40. $filenamePartsCount = count($filenameParts);
  41. if (strcasecmp($filenameParts[$filenamePartsCount-1], "tgz") == 0)
  42. return "tar.gz";
  43. else if (strcasecmp($filenameParts[$filenamePartsCount-1], "tbz2") == 0)
  44. return "tar.bz2";
  45. else if(strcasecmp($filenameParts[$filenamePartsCount-2], "tar") == 0) {
  46. if(strcasecmp($filenameParts[$filenamePartsCount-1], "gz") == 0)
  47. return "tar.gz";
  48. else if(strcasecmp($filenameParts[$filenamePartsCount-1], "bz2") == 0)
  49. return "tar.bz2";
  50. }
  51. else {
  52. return strtolower($filenameParts[$filenamePartsCount-1]);
  53. }
  54. }
  55. /** Function to unzip $file in $dir to $destination with $permissions
  56. *
  57. * modified candido1212 at yahoo dot com dot br's code on php.net manual
  58. * http://us3.php.net/zip
  59. * @param path, filename, destination, permissions (refer to mkdir)
  60. */
  61.  
  62. function unzip($dir, $file, $destination="", $permissions = 0755)
  63. {
  64. if (substr($dir, -1) != DIRECTORY_SEPARATOR) $dir .= DIRECTORY_SEPARATOR;
  65. if (substr($destination, -1) != DIRECTORY_SEPARATOR) $destination .= DIRECTORY_SEPARATOR;
  66. $path_file = $dir . $file;
  67. $zip = zip_open($path_file);
  68. $_tmp = array();
  69. if ($zip)
  70. {
  71. while ($zip_entry = zip_read($zip))
  72. {
  73. if (zip_entry_open($zip, $zip_entry, "r"))
  74. {
  75. if($destination)
  76. {
  77. $path_file = $destination . zip_entry_name($zip_entry);
  78. }
  79. else
  80. {
  81. $path_file = $dir . zip_entry_name($zip_entry);
  82. }
  83. $new_dir = dirname($path_file);
  84.  
  85. // Create Recursive Directory
  86. mkdirr($new_dir, $permissions);
  87. //change: do not try to read file if is a directory
  88. if (!(substr(zip_entry_name($zip_entry), -1) == "/")) {
  89. $fp = fopen($path_file, "w");
  90. while ($buf = zip_entry_read($zip_entry, 4096))
  91. fwrite($fp, $buf, 4096);
  92. fclose($fp);
  93. $_tmp[] = $path_file;
  94. }
  95. zip_entry_close($zip_entry);
  96. }
  97. }
  98. zip_close($zip);
  99. }
  100. return $_tmp;
  101. }
  102. /**
  103. * Uncompresses the archive appropriate to its filetype to the given path.
  104. *
  105. * @param string $filename
  106. * @return boolean
  107. * @access public
  108. * @since 06/10/05
  109. *
  110. */
  111. function uncompressFile($filename, $path) {
  112. $fileType = $this->_getFileType($filename);
  113. switch ($fileType) {
  114. case "tar":
  115. $tar = new Archive_Tar($filename);
  116. $tar->extract($path);
  117. unlink($filename);
  118. break;
  119. case "tar.gz":
  120. $tar = new Archive_Tar($filename, "gz");
  121. $tar->extract($path);
  122. unlink($filename);
  123. break;
  124. case "tar.bz2":
  125. $tar = new Archive_Tar($filename, "bz2");
  126. $tar->extract($path);
  127. unlink($filename);
  128. break;
  129. case "zip":
  130. $this->unzip(dirname($filename), basename($filename), $path,
  131. 0700);
  132. unlink($filename);
  133. break;
  134. case "gz":
  135. $file = gzopen($filename, "rb");
  136. $outFile = fopen($path, "wb");
  137. while (!gzeof($file)) {
  138. $buffer = gzread($file, 4096);
  139. fwrite ($outFile, $buffer, 4096);
  140. }
  141. gzclose($file);
  142. fclose($outFile);
  143. unlink($filename);
  144. break;
  145. case "bz2":
  146. $file = bzopen($filename, "rb");
  147. $outFile = fopen($path. "wb");
  148. while ($buffer = bzread($file, 4096))
  149. fwrite($outFile, $buffer, 4096);
  150. fclose($outFile);
  151. bzclose($file);
  152. unlink($filename);
  153. break;
  154. default:
  155. return false;
  156. }
  157. return true;
  158. }
  159. }
  160. ?>

Documentation generated on Wed, 19 Sep 2007 10:22:39 -0400 by phpDocumentor 1.3.0RC3