Skip to content

Commit

Permalink
Adding .rar archive (#4)
Browse files Browse the repository at this point in the history
Adding .rar archive support by @xc0d3rz
  • Loading branch information
xC0d3rZ authored and ndeet committed Jul 16, 2016
1 parent d76ccb0 commit 734340d
Showing 1 changed file with 49 additions and 12 deletions.
61 changes: 49 additions & 12 deletions unzipper.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,19 @@ public function __construct() {
if ($dh = opendir($this->localdir)) {
while (($file = readdir($dh)) !== FALSE) {
if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
|| pathinfo($file, PATHINFO_EXTENSION) === 'gz'
|| pathinfo($file, PATHINFO_EXTENSION) === 'gz'
|| pathinfo($file, PATHINFO_EXTENSION) === 'rar'
) {
$this->zipfiles[] = $file;
}
}
closedir($dh);

if (!empty($this->zipfiles)) {
self::$status = '.zip or .gz files found, ready for extraction';
self::$status = '.zip or .gz or .rar files found, ready for extraction';
}
else {
self::$status = '<span class="status--ERROR">Error: No .zip or .gz files found.</span>';
self::$status = '<span class="status--ERROR">Error: No .zip or .gz or rar files found.</span>';
}
}

Expand Down Expand Up @@ -76,15 +77,18 @@ public function __construct() {
*/
public static function extract($archive, $destination) {
$ext = pathinfo($archive, PATHINFO_EXTENSION);
if ($ext === 'zip') {
self::extractZipArchive($archive, $destination);
switch ($ext) {
case 'zip':
self::extractZipArchive($archive, $destination);
break;
case 'gz':
self::extractGzipFile($archive, $destination);
break;
case 'rar':
self::extractRarArchive($archive, $destination);
break;
}
else {
if ($ext === 'gz') {
self::extractGzipFile($archive, $destination);
}
}


}

/**
Expand Down Expand Up @@ -151,6 +155,39 @@ public static function extractGzipFile($archive, $destination) {
}

}

/**
* Decompress/extract a Rar archive using RarArchive.
*
* @param $archive
* @param $destination
*/
public static function extractRarArchive($archive, $destination) {
// Check if webserver supports unzipping.
if (!class_exists('RarArchive')) {
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>';
return;
}
// Check if archive is readable.
if ($rar = RarArchive::open($archive)) {
// Check if destination is writable
if (is_writeable($destination . '/')) {
$entries = $rar->getEntries();
foreach ($entries as $entry) {
$entry->extract($destination);
}
$rar->close();
self::$status = '<span class="status--OK">Files extracted successfully</span>';
}
else {
self::$status = '<span class="status--ERROR">Error: Directory not writeable by webserver.</span>';
}
}
else {
self::$status = '<span class="status--ERROR">Error: Cannot read .rar archive.</span>';
}
}

}

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

0 comments on commit 734340d

Please sign in to comment.