PHP unzipper / extracts .zip archives and .gz files on webservers

<?php
$timestart = microtime(TRUE);

$arc = new Unzipper;

$timeend = microtime(TRUE);
$time = $timeend - $timestart;

class Unzipper {
  public $localdir = '.';
  public $zipfiles = array();
  public static $status = '';

  public function __construct() {

    //read directory and pick .zip and .gz files
    if ($dh = opendir($this->localdir)) {
      while (($file = readdir($dh)) !== FALSE) {
        if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
          || pathinfo($file, PATHINFO_EXTENSION) === 'gz'
        ) {
          $this->zipfiles[] = $file;
        }
      }
      closedir($dh);

      if(!empty($this->zipfiles)) {
        self::$status = '.zip or .gz files found, ready for extraction';
      }
      else {
        self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: No .zip or .gz files found.</span>';
      }
    }

    //check if an archive was selected for unzipping
    //check if archive has been selected
    $input = '';
    $input = strip_tags($_POST['zipfile']);

    //allow only local existing archives to extract
    if ($input !== '') {
      if (in_array($input, $this->zipfiles)) {
        self::extract($input, $this->localdir);
      }
    }
  }

  public static function extract($archive, $destination) {
    $ext = pathinfo($archive, PATHINFO_EXTENSION);

    if ($ext === 'zip') {
      self::extractZipArchive($archive, $destination);
    }
    else {
      if ($ext === 'gz') {
        self::extractGzipFile($archive, $destination);
      }
    }

  }

  /**
   * Decompress/extract a zip archive using ZipArchive.
   *
   * @param $archive
   * @param $destination
   */
  public static function extractZipArchive($archive, $destination) {
    // Check if webserver supports unzipping.
    if(!class_exists('ZipArchive')) {
      self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Your PHP version does not support unzip functionality.</span>';
      return;
    }

    $zip = new ZipArchive;

    // Check if archive is readable.
    if ($zip->open($archive) === TRUE) {
      // Check if destination is writable
      if(is_writeable($destination . '/')) {
        $zip->extractTo($destination);
        $zip->close();
        self::$status = '<span style="color:green; font-weight:bold;font-size:120%;">Files unzipped successfully</span>';
      }
      else {
        self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Directory not writeable by webserver.</span>';
      }
    }
    else {
      self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Cannot read .zip archive.</span>';
    }
  }

  /**
   * Decompress a .gz File.
   *
   * @param $archive
   * @param $destination
   */
  public static function extractGzipFile($archive, $destination) {
    // Check if zlib is enabled
    if(!function_exists('gzopen')) {
      self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Your PHP has no zlib support enabled.</span>';
      return;
    }

    $filename = pathinfo($archive, PATHINFO_FILENAME);
    $gzipped = gzopen($archive, "rb");
    $file = fopen($filename, "w");

    while ($string = gzread($gzipped, 4096)) {
      fwrite($file, $string, strlen($string));
    }
    gzclose($gzipped);
    fclose($file);

    // Check if file was extracted.
    if(file_exists($destination . '/' . $filename)) {
      self::$status = '<span style="color:green; font-weight:bold;font-size:120%;">File unzipped successfully.</span>';
    }
    else {
      self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error unzipping file.</span>';
    }

  }
}

?>

<form action="" method="POST">
  <fieldset>

    <select name="zipfile" size="1" class="select">
      <?php foreach ($arc->zipfiles as $zip) {
        echo "<option>$zip</option>";
      }
      ?>
    </select>

    <br/>

    <input type="submit" name="submit" class="submit" value="Unzip Archive"/>

  </fieldset>
</form>
<p class="status">
  Status: <?php echo $arc::$status; ?>
  <br/>
  Processingtime: <?php echo $time; ?> ms
</p>

 

How to choose a random word from an array php
<?php $Words =array("Black","Yellow","Blue","Red","Purple","Pink", "White"); $rand_word=array_rand($Words,2); $Result =
How to minify HTML code of PHP page
<?php ob_start("minifier"); function minifier($code) { $search =
How to copy a file from one directory to another using PHP
<?php // Copy the file from /user/desktop/geek.txt
How to Deleting all files from a folder using PHP
<?php // PHP program to delete all
How do I test if an remote image file exists in php
if(@get_headers($your_url) == 'HTTP/1.1 404 Not Found') {
Human Readable File Size with PHP
function bytesToHuman($bytes) { $units = ['B', 'KB',
PHP Simple File Upload from Remote URL
<?php if($_POST){ //get the url $url =
Get the first character in a string with PHP
//Example string. $string = "My milkshake brings
How to generate a random token with PHP
//Generate a random string. $token = openssl_random_pseudo_bytes(16);
How to handle HTML checkboxes with PHP
<?php //Set "terms accepted" to false by
Generating random names with PHP
<?php //PHP array containing forenames. $names =
How to create PHP Timer Countdown
//You must call the function session_start() before
PHP – Do not echo out HTML!
Wrong: <?php echo '<div id="user_profile">'; echo '<h1>'
How to rotate a series of words on page load using PHP
$words = array('fox', 'hippo', 'dog', 'cat'); echo
How to check if a value exists in an array in PHP
$zoo = array("Lion", "Elephant", "Tiger", "Zebra", "Rhino",
How to Get the Current Date and Time in PHP
date_default_timezone_set('Asia/Kolkata'); $date = date('d-m-y h:i:s'); echo $date;
How to remove HTML special characters from a string in PHP
$my_str = "String with <b>special</b> characters."; //
How to extract substring from a string in PHP
$str = "Hello World!"; echo substr($str, 0,
How to Get Current Page URL in PHP
PHP program to get complete URL of

Top