PageRenderTime 81ms CodeModel.GetById 26ms RepoModel.GetById 1ms app.codeStats 0ms

/libraries/joomla/filesystem/archive.php

https://github.com/chrisinammo/arthurmcneil
PHP | 189 lines | 139 code | 12 blank | 38 comment | 23 complexity | 517a0cc7e43385c5f640e63e248c3d71 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-1.0
  1. <?php
  2. /**
  3. * @version $Id: archive.php 9764 2007-12-30 07:48:11Z ircmaxell $
  4. * @package Joomla.Framework
  5. * @subpackage FileSystem
  6. * @copyright Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
  7. * @license GNU/GPL, see LICENSE.php
  8. * Joomla! is free software. This version may have been modified pursuant
  9. * to the GNU General Public License, and as distributed it includes or
  10. * is derivative of works licensed under the GNU General Public License or
  11. * other free or open source software licenses.
  12. * See COPYRIGHT.php for copyright notices and details.
  13. */
  14. /**
  15. * An Archive handling class
  16. *
  17. * @static
  18. * @package Joomla.Framework
  19. * @subpackage FileSystem
  20. * @since 1.5
  21. */
  22. class JArchive
  23. {
  24. /**
  25. * @param string The name of the archive file
  26. * @param string Directory to unpack into
  27. * @return boolean True for success
  28. */
  29. function extract( $archivename, $extractdir)
  30. {
  31. jimport('joomla.filesystem.file');
  32. jimport('joomla.filesystem.folder');
  33. $untar = false;
  34. $result = false;
  35. $ext = JFile::getExt(strtolower($archivename));
  36. // check if a tar is embedded...gzip/bzip2 can just be plain files!
  37. if (JFile::getExt(JFile::stripExt(strtolower($archivename))) == 'tar') {
  38. $untar = true;
  39. }
  40. switch ($ext)
  41. {
  42. case 'zip':
  43. $adapter =& JArchive::getAdapter('zip');
  44. if ($adapter) {
  45. $result = $adapter->extract($archivename, $extractdir);
  46. }
  47. break;
  48. case 'tar':
  49. $adapter =& JArchive::getAdapter('tar');
  50. if ($adapter) {
  51. $result = $adapter->extract($archivename, $extractdir);
  52. }
  53. break;
  54. case 'tgz' :
  55. $untar = true; // This format is a tarball gzip'd
  56. case 'gz' : // This may just be an individual file (e.g. sql script)
  57. case 'gzip' :
  58. $adapter =& JArchive::getAdapter('gzip');
  59. if ($adapter)
  60. {
  61. $config =& JFactory::getConfig();
  62. $tmpfname = $config->getValue('config.tmp_path').DS.uniqid('gzip');
  63. $gzresult = $adapter->extract($archivename, $tmpfname);
  64. if (JError::isError($gzresult))
  65. {
  66. @unlink($tmpfname);
  67. return false;
  68. }
  69. if($untar)
  70. {
  71. // Try to untar the file
  72. $tadapter =& JArchive::getAdapter('tar');
  73. if ($tadapter) {
  74. $result = $tadapter->extract($tmpfname, $extractdir);
  75. }
  76. }
  77. else
  78. {
  79. $path = JPath::clean($extractdir);
  80. JFolder::create($path);
  81. $result = JFile::copy($tmpfname,$path.DS.JFile::stripExt(JFile::getName(strtolower($archivename))));
  82. }
  83. @unlink($tmpfname);
  84. }
  85. break;
  86. case 'tbz2' :
  87. $untar = true; // This format is a tarball bzip2'd
  88. case 'bz2' : // This may just be an individual file (e.g. sql script)
  89. case 'bzip2':
  90. $adapter =& JArchive::getAdapter('bzip2');
  91. if ($adapter)
  92. {
  93. $config =& JFactory::getConfig();
  94. $tmpfname = $config->getValue('config.tmp_path').DS.uniqid('bzip2');
  95. $bzresult = $adapter->extract($archivename, $tmpfname);
  96. if (JError::isError($bzresult))
  97. {
  98. @unlink($tmpfname);
  99. return false;
  100. }
  101. if ($untar)
  102. {
  103. // Try to untar the file
  104. $tadapter =& JArchive::getAdapter('tar');
  105. if ($tadapter) {
  106. $result = $tadapter->extract($tmpfname, $extractdir);
  107. }
  108. }
  109. else
  110. {
  111. $path = JPath::clean($extractdir);
  112. JFolder::create($path);
  113. $result = JFile::copy($tmpfname,$path.DS.JFile::stripExt(JFile::getName(strtolower($archivename))));
  114. }
  115. @unlink($tmpfname);
  116. }
  117. break;
  118. default:
  119. JError::raiseWarning(10, JText::_('UNKNOWNARCHIVETYPE'));
  120. return false;
  121. break;
  122. }
  123. if (! $result || JError::isError($result)) {
  124. return false;
  125. }
  126. return true;
  127. }
  128. function &getAdapter($type)
  129. {
  130. static $adapters;
  131. if (!isset($adapters)) {
  132. $adapters = array();
  133. }
  134. if (!isset($adapters[$type]))
  135. {
  136. // Try to load the adapter object
  137. $class = 'JArchive'.ucfirst($type);
  138. if (!class_exists($class))
  139. {
  140. $path = dirname(__FILE__).DS.'archive'.DS.strtolower($type).'.php';
  141. if (file_exists($path)) {
  142. require_once($path);
  143. } else {
  144. JError::raiseError(500,JText::_('Unable to load archive'));
  145. }
  146. }
  147. $adapters[$type] = new $class();
  148. }
  149. return $adapters[$type];
  150. }
  151. /**
  152. * @param string The name of the archive
  153. * @param mixed The name of a single file or an array of files
  154. * @param string The compression for the archive
  155. * @param string Path to add within the archive
  156. * @param string Path to remove within the archive
  157. * @param boolean Automatically append the extension for the archive
  158. * @param boolean Remove for source files
  159. */
  160. function create($archive, $files, $compress = 'tar', $addPath = '', $removePath = '', $autoExt = false, $cleanUp = false)
  161. {
  162. jimport( 'pear.archive_tar.Archive_Tar' );
  163. if (is_string($files)) {
  164. $files = array ($files);
  165. }
  166. if ($autoExt) {
  167. $archive .= '.'.$compress;
  168. }
  169. $tar = new Archive_Tar( $archive, $compress );
  170. $tar->setErrorHandling(PEAR_ERROR_PRINT);
  171. $tar->createModify( $files, $addPath, $removePath );
  172. if ($cleanUp) {
  173. JFile::delete( $files );
  174. }
  175. return $tar;
  176. }
  177. }