Draco File Unzipper


SUBMITTED BY: mgalarm

DATE: Oct. 1, 2016, 10:59 p.m.

FORMAT: Text only

SIZE: 6.7 kB

HITS: 957

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

comments powered by Disqus