Advertisement
kingdraco

Draco File Unzipper

Oct 1st, 2016
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.33 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Single PHP Unzip .zip arsip dan .gz pada webserver. dapat di tangani secara simple
  4.  * @author  King Draco, draco@serverindo.asia
  5.  * @version 1.2.2 Stable
  6.  */
  7. $timestart = microtime(TRUE);
  8. $arc = new Unzipper;
  9. $timeend = microtime(TRUE);
  10. $time = $timeend - $timestart;
  11. class Unzipper {
  12.   public $localdir = '.';
  13.   public $zipfiles = array();
  14.   public static $status = '';
  15.   public function __construct() {
  16.     //read directory and pick .zip and .gz files
  17.     if ($dh = opendir($this->localdir)) {
  18.       while (($file = readdir($dh)) !== FALSE) {
  19.         if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
  20.           || pathinfo($file, PATHINFO_EXTENSION) === 'gz'
  21.         ) {
  22.           $this->zipfiles[] = $file;
  23.         }
  24.       }
  25.       closedir($dh);
  26.       if(!empty($this->zipfiles)) {
  27.         self::$status = '.zip or .gz files ditemukan, siap untuk membongkar';
  28.       }
  29.       else {
  30.         self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Tidak ada file .zip atau .gz ditemukan.</span>';
  31.       }
  32.     }
  33.     //check file archive terlebih dahulu
  34.     //check juga archive yang telah di pilih
  35.     $input = '';
  36.     $input = strip_tags($_POST['zipfile']);
  37.     //hanya mengizinkan arsip lokal untuk dibongkar
  38.     if ($input !== '') {
  39.       if (in_array($input, $this->zipfiles)) {
  40.         self::extract($input, $this->localdir);
  41.       }
  42.     }
  43.   }
  44.   public static function extract($archive, $destination) {
  45.     $ext = pathinfo($archive, PATHINFO_EXTENSION);
  46.     if ($ext === 'zip') {
  47.       self::extractZipArchive($archive, $destination);
  48.     }
  49.     else {
  50.       if ($ext === 'gz') {
  51.         self::extractGzipFile($archive, $destination);
  52.       }
  53.     }
  54.   }
  55.   /**
  56.    * Decompress/extract a zip archive using ZipArchive.
  57.    *
  58.    * @param $archive
  59.    * @param $destination
  60.    */
  61.   public static function extractZipArchive($archive, $destination) {
  62.     // Check if webserver supports unzipping.
  63.     if(!class_exists('ZipArchive')) {
  64.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Your PHP version does not support unzip functionality.</span>';
  65.       return;
  66.     }
  67.     $zip = new ZipArchive;
  68.     // Check if archive is readable.
  69.     if ($zip->open($archive) === TRUE) {
  70.       // Check if destination is writable
  71.       if(is_writeable($destination . '/')) {
  72.         $zip->extractTo($destination);
  73.         $zip->close();
  74.         self::$status = '<span style="color:green; font-weight:bold;font-size:120%;">Files unzipped successfully</span>';
  75.       }
  76.       else {
  77.         self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Directory not writeable by webserver.</span>';
  78.       }
  79.     }
  80.     else {
  81.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Cannot read .zip archive.</span>';
  82.     }
  83.   }
  84.   /**
  85.    * Decompress a .gz File.
  86.    *
  87.    * @param $archive
  88.    * @param $destination
  89.    */
  90.   public static function extractGzipFile($archive, $destination) {
  91.     // Check if zlib is enabled
  92.     if(!function_exists('gzopen')) {
  93.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error: Your PHP has no zlib support enabled.</span>';
  94.       return;
  95.     }
  96.     $filename = pathinfo($archive, PATHINFO_FILENAME);
  97.     $gzipped = gzopen($archive, "rb");
  98.     $file = fopen($filename, "w");
  99.     while ($string = gzread($gzipped, 4096)) {
  100.       fwrite($file, $string, strlen($string));
  101.     }
  102.     gzclose($gzipped);
  103.     fclose($file);
  104.     // Check if file was extracted.
  105.     if(file_exists($destination . '/' . $filename)) {
  106.       self::$status = '<span style="color:green; font-weight:bold;font-size:120%;">File unzipped successfully.</span>';
  107.     }
  108.     else {
  109.       self::$status = '<span style="color:red; font-weight:bold;font-size:120%;">Error unzipping file.</span>';
  110.     }
  111.   }
  112. }
  113. ?>
  114.  
  115. <!DOCTYPE html>
  116. <head>
  117.   <title>Draco File Unzipper</title>
  118.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  119.   <style type="text/css">
  120.     <!--
  121.     body {
  122.       font-family: Arial, serif;
  123.       line-height: 150%;
  124.     }
  125.     fieldset {
  126.       border: 0px solid #000;
  127.     }
  128.     .select {
  129.       padding: 5px;
  130.       font-size: 110%;
  131.     }
  132.     .status {
  133.       margin-top: 20px;
  134.       padding: 5px;
  135.       font-size: 80%;
  136.       background: #EEE;
  137.       border: 1px dotted #DDD;
  138.     }
  139.     .submit {
  140.       -moz-box-shadow: inset 0px 1px 0px 0px #bbdaf7;
  141.       -webkit-box-shadow: inset 0px 1px 0px 0px #bbdaf7;
  142.       box-shadow: inset 0px 1px 0px 0px #bbdaf7;
  143.       background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #79bbff), color-stop(1, #378de5));
  144.       background: -moz-linear-gradient(center top, #79bbff 5%, #378de5 100%);
  145.       filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#79bbff', endColorstr='#378de5');
  146.       background-color: #79bbff;
  147.       -moz-border-radius: 4px;
  148.       -webkit-border-radius: 4px;
  149.       border-radius: 4px;
  150.       border: 1px solid #84bbf3;
  151.       display: inline-block;
  152.       color: #ffffff;
  153.       font-family: arial;
  154.       font-size: 15px;
  155.       font-weight: bold;
  156.       padding: 10px 24px;
  157.       text-decoration: none;
  158.       text-shadow: 1px 1px 0px #528ecc;
  159.     }
  160.     .submit:hover {
  161.       background: -webkit-gradient(linear, left top, left bottom, color-stop(0.05, #378de5), color-stop(1, #79bbff));
  162.       background: -moz-linear-gradient(center top, #378de5 5%, #79bbff 100%);
  163.       filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#378de5', endColorstr='#79bbff');
  164.       background-color: #378de5;
  165.     }
  166.     .submit:active {
  167.       position: relative;
  168.       top: 1px;
  169.     }
  170.     /* This imageless css button was generated by CSSButtonGenerator.com */
  171.     -->
  172.   </style>
  173. </head>
  174.  
  175. <body>
  176. <h1>Draco Archive Unzipper</h1>
  177.  
  178. <p>Pilih Arsip .zip atau .gz file yg ingin kamu extrack:</p>
  179.  
  180. <form action="" method="POST">
  181.   <fieldset>
  182.  
  183.     <select name="zipfile" size="1" class="select">
  184.       <?php foreach ($arc->zipfiles as $zip) {
  185.         echo "<option>$zip</option>";
  186.       }
  187.       ?>
  188.     </select>
  189.  
  190.     <br/>
  191.  
  192.     <input type="submit" name="submit" class="submit" value="Unzip Archive"/>
  193.  
  194.   </fieldset>
  195. </form>
  196. <p class="status">
  197.   Status: <?php echo $arc::$status; ?>
  198.   <br/>
  199.   Processingtime: <?php echo $time; ?> ms
  200. </p>
  201. </body>
  202. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement