Skip to content

Commit 734340d

Browse files
xC0d3rZndeet
authored andcommitted
Adding .rar archive (#4)
Adding .rar archive support by @xc0d3rz
1 parent d76ccb0 commit 734340d

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

unzipper.php

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,18 +32,19 @@ public function __construct() {
3232
if ($dh = opendir($this->localdir)) {
3333
while (($file = readdir($dh)) !== FALSE) {
3434
if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
35-
|| pathinfo($file, PATHINFO_EXTENSION) === 'gz'
35+
|| pathinfo($file, PATHINFO_EXTENSION) === 'gz'
36+
|| pathinfo($file, PATHINFO_EXTENSION) === 'rar'
3637
) {
3738
$this->zipfiles[] = $file;
3839
}
3940
}
4041
closedir($dh);
4142

4243
if (!empty($this->zipfiles)) {
43-
self::$status = '.zip or .gz files found, ready for extraction';
44+
self::$status = '.zip or .gz or .rar files found, ready for extraction';
4445
}
4546
else {
46-
self::$status = '<span class="status--ERROR">Error: No .zip or .gz files found.</span>';
47+
self::$status = '<span class="status--ERROR">Error: No .zip or .gz or rar files found.</span>';
4748
}
4849
}
4950

@@ -76,15 +77,18 @@ public function __construct() {
7677
*/
7778
public static function extract($archive, $destination) {
7879
$ext = pathinfo($archive, PATHINFO_EXTENSION);
79-
if ($ext === 'zip') {
80-
self::extractZipArchive($archive, $destination);
80+
switch ($ext) {
81+
case 'zip':
82+
self::extractZipArchive($archive, $destination);
83+
break;
84+
case 'gz':
85+
self::extractGzipFile($archive, $destination);
86+
break;
87+
case 'rar':
88+
self::extractRarArchive($archive, $destination);
89+
break;
8190
}
82-
else {
83-
if ($ext === 'gz') {
84-
self::extractGzipFile($archive, $destination);
85-
}
86-
}
87-
91+
8892
}
8993

9094
/**
@@ -151,6 +155,39 @@ public static function extractGzipFile($archive, $destination) {
151155
}
152156

153157
}
158+
159+
/**
160+
* Decompress/extract a Rar archive using RarArchive.
161+
*
162+
* @param $archive
163+
* @param $destination
164+
*/
165+
public static function extractRarArchive($archive, $destination) {
166+
// Check if webserver supports unzipping.
167+
if (!class_exists('RarArchive')) {
168+
self::$status = '<span class="status--ERROR">Error: Your PHP version does not support Rar functionality.<a class="info" href="http://php.net/manual/en/rar.installation.php">How to install RarArchive</a></span>';
169+
return;
170+
}
171+
// Check if archive is readable.
172+
if ($rar = RarArchive::open($archive)) {
173+
// Check if destination is writable
174+
if (is_writeable($destination . '/')) {
175+
$entries = $rar->getEntries();
176+
foreach ($entries as $entry) {
177+
$entry->extract($destination);
178+
}
179+
$rar->close();
180+
self::$status = '<span class="status--OK">Files extracted successfully</span>';
181+
}
182+
else {
183+
self::$status = '<span class="status--ERROR">Error: Directory not writeable by webserver.</span>';
184+
}
185+
}
186+
else {
187+
self::$status = '<span class="status--ERROR">Error: Cannot read .rar archive.</span>';
188+
}
189+
}
190+
154191
}
155192

156193
?>
@@ -234,7 +271,7 @@ public static function extractGzipFile($archive, $destination) {
234271
<h1>Archive Unzipper</h1>
235272
<form action="" method="POST">
236273
<fieldset>
237-
<label for="zipfile">Select .zip archive or .gz file you want to extract:</label>
274+
<label for="zipfile">Select .zip archive or .gz or .rar archive file you want to extract:</label>
238275
<select name="zipfile" size="1" class="select">
239276
<?php foreach ($arc->zipfiles as $zip) {
240277
echo "<option>$zip</option>";

0 commit comments

Comments
 (0)