Fast way for unzipping large libraries and frameworks on your ftp server

Fast way for unzipping large libraries and frameworks on your ftp server

Moving an unpacked large library or frameworks to a remote ftp server can take a long time. You can do this much faster with this small utility.

zip-1802627_1280.png

Moving large unzipped library or frameworks like Ext JS or Wordpress to a remote ftp server can be a real time consumer. This is because of the many small files in those distributions. With the small utility described below it can be done much faster.

Why not unzip on the server

It is much faster to upload the zipped library to your ftp server and unpack it there.

How to do this?

Create on your ftp server a directory called zipper. Make sure you can access it from your browser as www.yoururl.com/zipper.

In this directory create a file called unzip.php in that directory (or some other name) and paste the following code in this file and save it. You can also download it from the link at the bottom of this article.

<?php

if (isset($_POST['zip_file'])) {

    $filename = $_POST['zip_file'];
    $source = $_POST['zip_file'];
    $type = 'zip';

    $name = explode(".", $filename);
    $accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed');
    foreach ($accepted_types as $mime_type) {
        if ($mime_type == $type) {
            $okay = true;
            break;
        }
    }

    /* PHP current path */
    $path = dirname(__FILE__) . '/';  // absolute path to the directory where zipper.php is in
    $filenoext = basename($filename, '.zip');   // absolute path to the directory where zipper.php is in (lowercase)
    $filenoext = basename($filenoext, '.ZIP');  // absolute path to the directory where zipper.php is in (when uppercase)
    $targetdir = $path . $filenoext; // target directory
    $targetzip = $path . $filename; // target zip file

    /* create directory if not exists', otherwise overwrite */
    /* target directory is same as filename without extension */
    if (is_dir($targetdir))
        rmdir_recursive($targetdir);

    mkdir($targetdir, 0777);

    /* here it is really happening */
    $zip = new ZipArchive();
    $x = $zip->open($targetzip);  // open the zip file to extract
    if ($x === true) {
        try {
            $zip->extractTo($targetdir); // place in the directory with same name
        } catch (Exception $e) {
            echo 'Something went wrong here, maybe nothing all is unzipped';
        }
        $zip->close();
        // unlink($targetzip);
        echo "Your .zip file was successfully unzipped.";
    }
}

echo '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">';
echo '<html xmlns="http://www.w3.org/1999/xhtml">';
echo '<head>';
echo '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />';
echo '<title>Unzip a zip file already on the web server</title>';
echo '</head>';
echo '<body>';
echo '<h1>Unzip a file already on the server</h1>';
echo '<br/>Use your favorite ftp program to upload large zip files to the server.<br/>';
echo 'Put it in the same directory as this unzip.php file, it is recommended to make a "zipper" directory.<br/>';
echo 'After uploading the file you select the file here and click the unzip button. It is unzipped in no time<br/>';

if ($message)
    echo '<p>$message</p>';

echo '<form method="post" action="">';
echo '<select name="zip_file">';

$dir = '.';
$dh = opendir($dir);
while (false !== ($fn = readdir($dh))) {
    $ext = substr($fn, strrpos($fn, '.') + 1);
    if (in_array($ext, array("zip", "ZIP"))) {
        echo '<option value="' . $fn . '">' . $fn . '</option>';
    }
}
echo '</select>';
echo '<br /><br />';
echo '<input type="submit" name="submit" value="Unzip" />';
echo '</form>';
echo '</body>';
echo '</html>';

function rmdir_recursive($dir) {
    foreach (scandir($dir) as $file) {
        if ('.' === $file || '..' === $file)
            continue;
        if (is_dir("$dir/$file"))
            rmdir_recursive("$dir/$file");
        else
            unlink("$dir/$file");
    }
    rmdir($dir);
}

How to use it?

Upload the zipped file that you want to unpack on your ftp server with your favorite ftp program (like Firefox fireftp) and put it in the root of the zipper folder.

When that is done, go to your browser and type www.yoururl.com/zipper/unzip.php.

It will show a dropdown with zip files in this directory. Select the file you want to unpack and click the Unzip button. Wait until the unzip process is done, it will show a message on top of the display that the file was successfully unzipped.

Return to your ftp program and refresh the zipper folder. In this folder you will also see a folder with the same name as your zip file. This folder can now easily be moved to the right place on the ftp server. Uploading and unpacking the Ext JS library (70 mB) was done under 3 minutes with a wireless connection and no more than 600 kB upload speed.

Small remarks

The reason that you still have to use an ftp program to upload the file to your server is to avoid upload limitations on the size. Some hosting providers don't allow large uploads through your browser.

Just zip

Only files in zip (.zip) format are supported. If you would like to extend it with RAR files, you have to check if the Rar class is loaded on the PHP configuration of your server (by default it isn't).

Remove the zip file after you have unpacked it. Don't use your zipper folder for other purposes than a working folder for the function described above.

Link

Unzip.php (download as rar file)

No comments yet

your email address will not be published. required fields are marked *