Advertisement
EddieKidiw

Php unziper

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