1
<?php
2
/**
3
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7
 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
14
 *
15
 * This software consists of voluntary contributions made by many individuals
16
 * and is licensed under the LGPL. For more information please see
17
 * <http://phing.info>.
18
 */
19

20
/**
21
 * Extracts one or several tar archives using PEAR Archive_Tar
22
 *
23
 * @author  Joakim Bodin <joakim.bodin+phing@gmail.com>
24
 * @package phing.tasks.ext
25
 * @since   2.2.0
26
 */
27
class UntarTask extends ExtractBaseTask
28
{
29

30
    /**
31
     * @var bool
32
     */
33
    private $preservePermissions = false;
34

35
    /**
36
     * @param bool $preservePermissions
37
     */
38 0
    public function setPreservePermissions($preservePermissions)
39
    {
40 0
        $this->preservePermissions = $preservePermissions;
41
    }
42

43
    /**
44
     * Ensures that PEAR lib exists.
45
     */
46 0
    public function init()
47
    {
48 0
        include_once 'Archive/Tar.php';
49 0
        if (!class_exists('Archive_Tar')) {
50 0
            throw new BuildException("You must have installed the PEAR Archive_Tar class in order to use UntarTask.");
51
        }
52
    }
53

54
    /**
55
     * @param PhingFile $tarfile
56
     * @return mixed|void
57
     * @throws BuildException
58
     */
59 0
    protected function extractArchive(PhingFile $tarfile)
60
    {
61 0
        $this->log(
62 0
            "Extracting tar file: " . $tarfile->__toString() . ' to ' . $this->todir->__toString(),
63 0
            Project::MSG_INFO
64
        );
65

66
        try {
67 0
            $tar = $this->initTar($tarfile);
68 0
            if (!$tar->extractModify($this->todir->getAbsolutePath(), $this->removepath, $this->preservePermissions)) {
69 0
                throw new BuildException('Failed to extract tar file: ' . $tarfile->getAbsolutePath() . '. Error: ' . $tar->error_object->getMessage());
70
            }
71 0
        } catch (IOException $ioe) {
72 0
            $msg = "Could not extract tar file: " . $ioe->getMessage();
73 0
            throw new BuildException($msg, $ioe, $this->getLocation());
74
        }
75
    }
76

77
    /**
78
     * @param PhingFile $tarfile
79
     * @return array|int
80
     */
81 0
    protected function listArchiveContent(PhingFile $tarfile)
82
    {
83 0
        $tar = $this->initTar($tarfile);
84

85 0
        return $tar->listContent();
86
    }
87

88
    /**
89
     * Init a Archive_Tar class with correct compression for the given file.
90
     *
91
     * @param  PhingFile $tarfile
92
     * @return Archive_Tar the tar class instance
93
     */
94 0
    private function initTar(PhingFile $tarfile)
95
    {
96 0
        $compression = null;
97 0
        $tarfileName = $tarfile->getName();
98 0
        $mode = strtolower(substr($tarfileName, strrpos($tarfileName, '.')));
99

100
        $compressions = [
101 0
            'gz' => ['.gz', '.tgz',],
102
            'bz2' => ['.bz2',],
103
        ];
104 0
        foreach ($compressions as $algo => $ext) {
105 0
            if (in_array($mode, $ext)) {
106 0
                $compression = $algo;
107 0
                break;
108
            }
109
        }
110

111 0
        return new Archive_Tar($tarfile->getAbsolutePath(), $compression);
112
    }
113
}

Read our documentation on viewing source code .

Loading