1 | <?php |
||
12 | class Archive |
||
13 | { |
||
14 | /** |
||
15 | * |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $tempPath; |
||
19 | |||
20 | /** |
||
21 | * |
||
22 | * @var string |
||
23 | */ |
||
24 | protected $archivePath; |
||
25 | |||
26 | /** |
||
27 | * @var \ZipArchive |
||
28 | */ |
||
29 | private $zip; |
||
30 | |||
31 | /** |
||
32 | * Constructor |
||
33 | * |
||
34 | * @param string $archivePath |
||
35 | */ |
||
36 | public function __construct($archivePath) |
||
42 | |||
43 | /** |
||
44 | * Extracts the specified file to a temp path, and return the temp path |
||
45 | * |
||
46 | * Files are only extracted once for the given archive |
||
47 | * |
||
48 | * @param string $filePath |
||
49 | * |
||
50 | * @return string |
||
51 | */ |
||
52 | public function extract($filePath) |
||
61 | |||
62 | /** |
||
63 | * Clears all extracted files |
||
64 | */ |
||
65 | public function __destruct() |
||
70 | |||
71 | /** |
||
72 | * Returns the archive |
||
73 | * |
||
74 | * @return \ZipArchive |
||
75 | */ |
||
76 | protected function getArchive() |
||
77 | { |
||
78 | if (!$this->zip) { |
||
79 | $this->zip = new \ZipArchive(); |
||
80 | if (true !== $errorCode = $this->zip->open($this->archivePath)) { |
||
81 | $this->zip = null; |
||
82 | throw new \RuntimeException('Error opening file: '.$this->getErrorMessage($errorCode)); |
||
83 | } |
||
84 | } |
||
85 | |||
86 | return $this->zip; |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * Closes the archive |
||
91 | */ |
||
92 | protected function closeArchive() |
||
99 | |||
100 | /** |
||
101 | * Deletes temporary files |
||
102 | */ |
||
103 | protected function deleteTemp() |
||
122 | |||
123 | private function getErrorMessage($errorCode) |
||
199 | } |
||
200 |