meetsos

PHP Zipper Unzipper

Jan 26th, 2023
32
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 12.40 KB | None | 0 0
  1. <?php
  2. /**
  3.  * The Unzipper extracts .zip or .rar archives and .gz files on webservers.
  4.  * It's handy if you do not have shell access. E.g. if you want to upload a lot
  5.  * of files (php framework or image collection) as an archive to save time.
  6.  * As of version 0.1.0 it also supports creating archives.
  7.  *
  8.  * @author  Andreas Tasch, at[tec], attec.at
  9.  * @license GNU GPL v3
  10.  * @package attec.toolbox
  11.  * @version 0.1.1
  12.  */
  13. define('VERSION', '0.1.1');
  14.  
  15. $timestart = microtime(TRUE);
  16. $GLOBALS['status'] = array();
  17.  
  18. $unzipper = new Unzipper;
  19. if (isset($_POST['dounzip'])) {
  20.   // Check if an archive was selected for unzipping.
  21.   $archive = isset($_POST['zipfile']) ? strip_tags($_POST['zipfile']) : '';
  22.   $destination = isset($_POST['extpath']) ? strip_tags($_POST['extpath']) : '';
  23.   $unzipper->prepareExtraction($archive, $destination);
  24. }
  25.  
  26. if (isset($_POST['dozip'])) {
  27.   $zippath = !empty($_POST['zippath']) ? strip_tags($_POST['zippath']) : '.';
  28.   // Resulting zipfile e.g. zipper--2016-07-23--11-55.zip.
  29.   $zipfile = 'zipper-' . date("Y-m-d--H-i") . '.zip';
  30.   Zipper::zipDir($zippath, $zipfile);
  31. }
  32.  
  33. $timeend = microtime(TRUE);
  34. $time = round($timeend - $timestart, 4);
  35.  
  36. /**
  37.  * Class Unzipper
  38.  */
  39. class Unzipper {
  40.   public $localdir = '.';
  41.   public $zipfiles = array();
  42.  
  43.   public function __construct() {
  44.     // Read directory and pick .zip, .rar and .gz files.
  45.     if ($dh = opendir($this->localdir)) {
  46.       while (($file = readdir($dh)) !== FALSE) {
  47.         if (pathinfo($file, PATHINFO_EXTENSION) === 'zip'
  48.           || pathinfo($file, PATHINFO_EXTENSION) === 'gz'
  49.           || pathinfo($file, PATHINFO_EXTENSION) === 'rar'
  50.         ) {
  51.           $this->zipfiles[] = $file;
  52.         }
  53.       }
  54.       closedir($dh);
  55.  
  56.       if (!empty($this->zipfiles)) {
  57.         $GLOBALS['status'] = array('info' => '.zip or .gz or .rar files found, ready for extraction');
  58.       }
  59.       else {
  60.         $GLOBALS['status'] = array('info' => 'No .zip or .gz or rar files found. So only zipping functionality available.');
  61.       }
  62.     }
  63.   }
  64.  
  65.   /**
  66.    * Prepare and check zipfile for extraction.
  67.    *
  68.    * @param string $archive
  69.    *   The archive name including file extension. E.g. my_archive.zip.
  70.    * @param string $destination
  71.    *   The relative destination path where to extract files.
  72.    */
  73.   public function prepareExtraction($archive, $destination = '') {
  74.     // Determine paths.
  75.     if (empty($destination)) {
  76.       $extpath = $this->localdir;
  77.     }
  78.     else {
  79.       $extpath = $this->localdir . '/' . $destination;
  80.       // Todo: move this to extraction function.
  81.       if (!is_dir($extpath)) {
  82.         mkdir($extpath);
  83.       }
  84.     }
  85.     // Only local existing archives are allowed to be extracted.
  86.     if (in_array($archive, $this->zipfiles)) {
  87.       self::extract($archive, $extpath);
  88.     }
  89.   }
  90.  
  91.   /**
  92.    * Checks file extension and calls suitable extractor functions.
  93.    *
  94.    * @param string $archive
  95.    *   The archive name including file extension. E.g. my_archive.zip.
  96.    * @param string $destination
  97.    *   The relative destination path where to extract files.
  98.    */
  99.   public static function extract($archive, $destination) {
  100.     $ext = pathinfo($archive, PATHINFO_EXTENSION);
  101.     switch ($ext) {
  102.       case 'zip':
  103.         self::extractZipArchive($archive, $destination);
  104.         break;
  105.       case 'gz':
  106.         self::extractGzipFile($archive, $destination);
  107.         break;
  108.       case 'rar':
  109.         self::extractRarArchive($archive, $destination);
  110.         break;
  111.     }
  112.  
  113.   }
  114.  
  115.   /**
  116.    * Decompress/extract a zip archive using ZipArchive.
  117.    *
  118.    * @param $archive
  119.    * @param $destination
  120.    */
  121.   public static function extractZipArchive($archive, $destination) {
  122.     // Check if webserver supports unzipping.
  123.     if (!class_exists('ZipArchive')) {
  124.       $GLOBALS['status'] = array('error' => 'Error: Your PHP version does not support unzip functionality.');
  125.       return;
  126.     }
  127.  
  128.     $zip = new ZipArchive;
  129.  
  130.     // Check if archive is readable.
  131.     if ($zip->open($archive) === TRUE) {
  132.       // Check if destination is writable
  133.       if (is_writeable($destination . '/')) {
  134.         $zip->extractTo($destination);
  135.         $zip->close();
  136.         $GLOBALS['status'] = array('success' => 'Files unzipped successfully');
  137.       }
  138.       else {
  139.         $GLOBALS['status'] = array('error' => 'Error: Directory not writeable by webserver.');
  140.       }
  141.     }
  142.     else {
  143.       $GLOBALS['status'] = array('error' => 'Error: Cannot read .zip archive.');
  144.     }
  145.   }
  146.  
  147.   /**
  148.    * Decompress a .gz File.
  149.    *
  150.    * @param string $archive
  151.    *   The archive name including file extension. E.g. my_archive.zip.
  152.    * @param string $destination
  153.    *   The relative destination path where to extract files.
  154.    */
  155.   public static function extractGzipFile($archive, $destination) {
  156.     // Check if zlib is enabled
  157.     if (!function_exists('gzopen')) {
  158.       $GLOBALS['status'] = array('error' => 'Error: Your PHP has no zlib support enabled.');
  159.       return;
  160.     }
  161.  
  162.     $filename = pathinfo($archive, PATHINFO_FILENAME);
  163.     $gzipped = gzopen($archive, "rb");
  164.     $file = fopen($destination . '/' . $filename, "w");
  165.  
  166.     while ($string = gzread($gzipped, 4096)) {
  167.       fwrite($file, $string, strlen($string));
  168.     }
  169.     gzclose($gzipped);
  170.     fclose($file);
  171.  
  172.     // Check if file was extracted.
  173.     if (file_exists($destination . '/' . $filename)) {
  174.       $GLOBALS['status'] = array('success' => 'File unzipped successfully.');
  175.  
  176.       // If we had a tar.gz file, let's extract that tar file.
  177.       if (pathinfo($destination . '/' . $filename, PATHINFO_EXTENSION) == 'tar') {
  178.         $phar = new PharData($destination . '/' . $filename);
  179.         if ($phar->extractTo($destination)) {
  180.           $GLOBALS['status'] = array('success' => 'Extracted tar.gz archive successfully.');
  181.           // Delete .tar.
  182.           unlink($destination . '/' . $filename);
  183.         }
  184.       }
  185.     }
  186.     else {
  187.       $GLOBALS['status'] = array('error' => 'Error unzipping file.');
  188.     }
  189.  
  190.   }
  191.  
  192.   /**
  193.    * Decompress/extract a Rar archive using RarArchive.
  194.    *
  195.    * @param string $archive
  196.    *   The archive name including file extension. E.g. my_archive.zip.
  197.    * @param string $destination
  198.    *   The relative destination path where to extract files.
  199.    */
  200.   public static function extractRarArchive($archive, $destination) {
  201.     // Check if webserver supports unzipping.
  202.     if (!class_exists('RarArchive')) {
  203.       $GLOBALS['status'] = array('error' => 'Error: Your PHP version does not support .rar archive functionality. <a class="info" href="http://php.net/manual/en/rar.installation.php" target="_blank">How to install RarArchive</a>');
  204.       return;
  205.     }
  206.     // Check if archive is readable.
  207.     if ($rar = RarArchive::open($archive)) {
  208.       // Check if destination is writable
  209.       if (is_writeable($destination . '/')) {
  210.         $entries = $rar->getEntries();
  211.         foreach ($entries as $entry) {
  212.           $entry->extract($destination);
  213.         }
  214.         $rar->close();
  215.         $GLOBALS['status'] = array('success' => 'Files extracted successfully.');
  216.       }
  217.       else {
  218.         $GLOBALS['status'] = array('error' => 'Error: Directory not writeable by webserver.');
  219.       }
  220.     }
  221.     else {
  222.       $GLOBALS['status'] = array('error' => 'Error: Cannot read .rar archive.');
  223.     }
  224.   }
  225.  
  226. }
  227.  
  228. /**
  229.  * Class Zipper
  230.  *
  231.  * Copied and slightly modified from http://at2.php.net/manual/en/class.ziparchive.php#110719
  232.  * @author umbalaconmeogia
  233.  */
  234. class Zipper {
  235.   /**
  236.    * Add files and sub-directories in a folder to zip file.
  237.    *
  238.    * @param string $folder
  239.    *   Path to folder that should be zipped.
  240.    *
  241.    * @param ZipArchive $zipFile
  242.    *   Zipfile where files end up.
  243.    *
  244.    * @param int $exclusiveLength
  245.    *   Number of text to be exclusived from the file path.
  246.    */
  247.   private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
  248.     $handle = opendir($folder);
  249.  
  250.     while (FALSE !== $f = readdir($handle)) {
  251.       // Check for local/parent path or zipping file itself and skip.
  252.       if ($f != '.' && $f != '..' && $f != basename(__FILE__)) {
  253.         $filePath = "$folder/$f";
  254.         // Remove prefix from file path before add to zip.
  255.         $localPath = substr($filePath, $exclusiveLength);
  256.  
  257.         if (is_file($filePath)) {
  258.           $zipFile->addFile($filePath, $localPath);
  259.         }
  260.         elseif (is_dir($filePath)) {
  261.           // Add sub-directory.
  262.           $zipFile->addEmptyDir($localPath);
  263.           self::folderToZip($filePath, $zipFile, $exclusiveLength);
  264.         }
  265.       }
  266.     }
  267.     closedir($handle);
  268.   }
  269.  
  270.   /**
  271.    * Zip a folder (including itself).
  272.    *
  273.    * Usage:
  274.    *   Zipper::zipDir('path/to/sourceDir', 'path/to/out.zip');
  275.    *
  276.    * @param string $sourcePath
  277.    *   Relative path of directory to be zipped.
  278.    *
  279.    * @param string $outZipPath
  280.    *   Relative path of the resulting output zip file.
  281.    */
  282.   public static function zipDir($sourcePath, $outZipPath) {
  283.     $pathInfo = pathinfo($sourcePath);
  284.     $parentPath = $pathInfo['dirname'];
  285.     $dirName = $pathInfo['basename'];
  286.  
  287.     $z = new ZipArchive();
  288.     $z->open($outZipPath, ZipArchive::CREATE);
  289.     $z->addEmptyDir($dirName);
  290.     if ($sourcePath == $dirName) {
  291.       self::folderToZip($sourcePath, $z, 0);
  292.     }
  293.     else {
  294.       self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
  295.     }
  296.     $z->close();
  297.  
  298.     $GLOBALS['status'] = array('success' => 'Successfully created archive ' . $outZipPath);
  299.   }
  300. }
  301. ?>
  302.  
  303. <!DOCTYPE html>
  304. <html>
  305. <head>
  306.   <title>File Unzipper + Zipper</title>
  307.   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  308.   <style type="text/css">
  309.     <!--
  310.     body {
  311.       font-family: Arial, sans-serif;
  312.       line-height: 150%;
  313.     }
  314.  
  315.     label {
  316.       display: block;
  317.       margin-top: 20px;
  318.     }
  319.  
  320.     fieldset {
  321.       border: 0;
  322.       background-color: #EEE;
  323.       margin: 10px 0 10px 0;
  324.     }
  325.  
  326.     .select {
  327.       padding: 5px;
  328.       font-size: 110%;
  329.     }
  330.  
  331.     .status {
  332.       margin: 0;
  333.       margin-bottom: 20px;
  334.       padding: 10px;
  335.       font-size: 80%;
  336.       background: #EEE;
  337.       border: 1px dotted #DDD;
  338.     }
  339.  
  340.     .status--ERROR {
  341.       background-color: red;
  342.       color: white;
  343.       font-size: 120%;
  344.     }
  345.  
  346.     .status--SUCCESS {
  347.       background-color: green;
  348.       font-weight: bold;
  349.       color: white;
  350.       font-size: 120%
  351.     }
  352.  
  353.     .small {
  354.       font-size: 0.7rem;
  355.       font-weight: normal;
  356.     }
  357.  
  358.     .version {
  359.       font-size: 80%;
  360.     }
  361.  
  362.     .form-field {
  363.       border: 1px solid #AAA;
  364.       padding: 8px;
  365.       width: 280px;
  366.     }
  367.  
  368.     .info {
  369.       margin-top: 0;
  370.       font-size: 80%;
  371.       color: #777;
  372.     }
  373.  
  374.     .submit {
  375.       background-color: #378de5;
  376.       border: 0;
  377.       color: #ffffff;
  378.       font-size: 15px;
  379.       padding: 10px 24px;
  380.       margin: 20px 0 20px 0;
  381.       text-decoration: none;
  382.     }
  383.  
  384.     .submit:hover {
  385.       background-color: #2c6db2;
  386.       cursor: pointer;
  387.     }
  388.     -->
  389.   </style>
  390. </head>
  391. <body>
  392. <p class="status status--<?php echo strtoupper(key($GLOBALS['status'])); ?>">
  393.   Status: <?php echo reset($GLOBALS['status']); ?><br/>
  394.   <span class="small">Processing Time: <?php echo $time; ?> seconds</span>
  395. </p>
  396. <form action="" method="POST">
  397.   <fieldset>
  398.     <h1>Archive Unzipper</h1>
  399.     <label for="zipfile">Select .zip or .rar archive or .gz file you want to extract:</label>
  400.     <select name="zipfile" size="1" class="select">
  401.       <?php foreach ($unzipper->zipfiles as $zip) {
  402.         echo "<option>$zip</option>";
  403.       }
  404.       ?>
  405.     </select>
  406.     <label for="extpath">Extraction path (optional):</label>
  407.     <input type="text" name="extpath" class="form-field" />
  408.     <p class="info">Enter extraction path without leading or trailing slashes (e.g. "mypath"). If left empty current directory will be used.</p>
  409.     <input type="submit" name="dounzip" class="submit" value="Unzip Archive"/>
  410.   </fieldset>
  411.  
  412.   <fieldset>
  413.     <h1>Archive Zipper</h1>
  414.     <label for="zippath">Path that should be zipped (optional):</label>
  415.     <input type="text" name="zippath" class="form-field" />
  416.     <p class="info">Enter path to be zipped without leading or trailing slashes (e.g. "zippath"). If left empty current directory will be used.</p>
  417.     <input type="submit" name="dozip" class="submit" value="Zip Archive"/>
  418.   </fieldset>
  419. </form>
  420. <p class="version">Unzipper version: <?php echo VERSION; ?></p>
  421. </body>
  422. </html>
Add Comment
Please, Sign In to add comment