diff --git a/unzipper.php b/unzipper.php index 7ca4bf8..aabce72 100644 --- a/unzipper.php +++ b/unzipper.php @@ -32,7 +32,8 @@ 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; } @@ -40,10 +41,10 @@ public function __construct() { 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 = 'Error: No .zip or .gz files found.'; + self::$status = 'Error: No .zip or .gz or rar files found.'; } } @@ -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); - } - } - + } /** @@ -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 = 'Error: Your PHP version does not support Rar functionality.How to install RarArchive'; + 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 = 'Files extracted successfully'; + } + else { + self::$status = 'Error: Directory not writeable by webserver.'; + } + } + else { + self::$status = 'Error: Cannot read .rar archive.'; + } + } + } ?> @@ -234,7 +271,7 @@ public static function extractGzipFile($archive, $destination) {