UntarTask.php 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. /*
  3. *
  4. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  5. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  6. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  7. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  8. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  9. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  10. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  11. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  12. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  13. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  14. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  15. *
  16. * This software consists of voluntary contributions made by many individuals
  17. * and is licensed under the LGPL. For more information please see
  18. * <http://phing.info>.
  19. */
  20. require_once 'phing/tasks/ext/ExtractBaseTask.php';
  21. /**
  22. * Extracts one or several tar archives using PEAR Archive_Tar
  23. *
  24. * @author Joakim Bodin <joakim.bodin+phing@gmail.com>
  25. * @version $Id: UntarTask.php 905 2010-10-05 16:28:03Z mrook $
  26. * @package phing.tasks.ext
  27. * @since 2.2.0
  28. */
  29. class UntarTask extends ExtractBaseTask {
  30. /**
  31. * Ensures that PEAR lib exists.
  32. */
  33. public function init() {
  34. include_once 'Archive/Tar.php';
  35. if (!class_exists('Archive_Tar')) {
  36. throw new BuildException("You must have installed the PEAR Archive_Tar class in order to use UntarTask.");
  37. }
  38. }
  39. protected function extractArchive(PhingFile $tarfile)
  40. {
  41. $this->log("Extracting tar file: " . $tarfile->__toString() . ' to ' . $this->todir->__toString(), Project::MSG_INFO);
  42. try {
  43. $tar = $this->initTar($tarfile);
  44. if(!$tar->extractModify($this->todir->getAbsolutePath(), $this->removepath)) {
  45. throw new BuildException('Failed to extract tar file: ' . $tarfile->getAbsolutePath());
  46. }
  47. } catch (IOException $ioe) {
  48. $msg = "Could not extract tar file: " . $ioe->getMessage();
  49. throw new BuildException($msg, $ioe, $this->getLocation());
  50. }
  51. }
  52. protected function listArchiveContent(PhingFile $tarfile)
  53. {
  54. $tar = $this->initTar($tarfile);
  55. return $tar->listContent();
  56. }
  57. /**
  58. * Init a Archive_Tar class with correct compression for the given file.
  59. *
  60. * @param PhingFile $tarfile
  61. * @return Archive_Tar the tar class instance
  62. */
  63. private function initTar(PhingFile $tarfile)
  64. {
  65. $compression = null;
  66. $tarfileName = $tarfile->getName();
  67. $mode = strtolower(substr($tarfileName, strrpos($tarfileName, '.')));
  68. $compressions = array(
  69. 'gz' => array('.gz', '.tgz',),
  70. 'bz2' => array('.bz2',),
  71. );
  72. foreach ($compressions as $algo => $ext) {
  73. if (array_search($mode, $ext) !== false) {
  74. $compression = $algo;
  75. break;
  76. }
  77. }
  78. return new Archive_Tar($tarfile->getAbsolutePath(), $compression);
  79. }
  80. }