Skip to content

Instantly share code, notes, and snippets.

@lrq3000
Created December 6, 2014 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lrq3000/fcf093a711f1a00ca825 to your computer and use it in GitHub Desktop.
Save lrq3000/fcf093a711f1a00ca825 to your computer and use it in GitHub Desktop.
Quickly coded zip importer in PHP with multiple methods, including one with chunking and auto-reloading for webhosts with agressive limits
<?php // Import a zip file and extract it
$custom_max_time = 10;
$custom_wait_time = 45;
$random_wait_time = 5; // plus or minus this value. Set to 0 or false to disable.
$time_start = time();
$max_time = ini_get("max_execution_time");
$wait_time = 5;
if (!empty($custom_max_time) and $custom_max_time > 0 and $custom_max_time < $max_time) $max_time = $custom_max_time;
if (!empty($custom_wait_time) and $custom_wait_time > 0) $wait_time = $custom_wait_time;
$url = $_POST['url'];
// get the absolute path to file url
$url = pathinfo(realpath($url), PATHINFO_DIRNAME).'/'.$url;
$name = $_POST['name'];
if (isset($_POST['unzip'])) {
$zip = new ZipArchive;
$res = $zip->open("$url");
if ($res === TRUE) {
$zip->extractTo('./');
$zip->close();
echo "zip file extracted!";
} else {
echo "ERROR: FILE NOT EXTRACTED!";
}
} elseif (isset($_POST['import'])) {
copy("$url","./$name");
} elseif (isset($_POST['unzip_chunks'])) {
$filename = $url;
$archive = zip_open($filename);
while($entry = zip_read($archive)){
$size = zip_entry_filesize($entry);
$name = zip_entry_name($entry);
$unzipped = fopen('./'.$name,'wb');
while($size > 0){
$chunkSize = ($size > 10240) ? 10240 : $size;
$size -= $chunkSize;
$chunk = zip_entry_read($entry, $chunkSize);
if($chunk !== false) fwrite($unzipped, $chunk);
}
fclose($unzipped);
}
} elseif (isset($_POST['unzip_chunks2']) or isset($_GET['unzip_chunks2'])) {
$orig_url = $_POST['url'];
if (empty($orig_url)) {
$orig_url = $_GET['url'];
}
if (isset($_GET['unzip_chunks2'])) {
$url = $_GET['url'];
$url = pathinfo(realpath($url), PATHINFO_DIRNAME).'/'.$url;
}
if (isset($_GET['last_index'])) {
$last_index = $_GET['last_index'];
} else {
$last_index = 0;
}
$zip = new ZipArchive;
if ($zip->open($url) === true) {
//make all the folders
if (!empty($last_index)) {
for($i = 0; $i < $zip->numFiles; $i++) {
$OnlyFileName = $zip->getNameIndex($i);
$FullFileName = $zip->statIndex($i);
if ($FullFileName['name'][strlen($FullFileName['name'])-1] =="/") {
@mkdir("./".$FullFileName['name'],0700,true);
}
}
}
//unzip into the folders
for($i = $last_index; $i < $zip->numFiles; $i++) {
if ( (time() - $time_start) > ($max_time-5) ) {
$zip->close();
$redirect_url = "http://".$_SERVER['HTTP_HOST'].$_SERVER['SCRIPT_NAME'].'?unzip_chunks2=true&url='.$orig_url.'&last_index='.$i;
$w_time = $wait_time;
if ($random_wait_time > 0) $w_time = $wait_time + rand(1, $random_wait_time);
?>
<html>
<head>
<meta http-equiv="refresh" content="<?php print($w_time) ?>; url=<?php print($redirect_url); ?>" />
</head>
<body>
The unzipping takes too much time, I have to split the work in several calls. You should be automatically redirected in <?php print($w_time) ?> seconds, if not, please <a href="<?php print($redirect_url); ?>">click here to continue the unzipping</a>.
</body>
</html>
<?php
exit(); // make sure we stop processing here
}
$OnlyFileName = $zip->getNameIndex($i);
$FullFileName = $zip->statIndex($i);
if (!($FullFileName['name'][strlen($FullFileName['name'])-1] =="/")) {
copy('zip://'. $url .'#'. $OnlyFileName , "./".$FullFileName['name'] );
}
}
$zip->close();
} else {
echo "Error: Can't open zip file: ".$url;
}
}
?>
<html>
<u><b>IMPORT</u></b><br />
<form method="post" action="">URL:<br /><input type="text" name="url"><br />SAVE AS:<br /><input name="name"><br /><input type="submit" name="import"></form>
<br /><u><b>UNZIP</u></b><br />*make sure zip file is inside public_html folder<br />
<form method="post" action="">FILE:<br /><input type="text" name="url" value="file.zip"><br /><input type="submit" name="unzip"></form>
<u><b>UNZIP BY CHUNKS</u></b><br />*make sure zip file is inside public_html folder<br />
<form method="post" action="">FILE:<br /><input type="text" name="url" value="file.zip"><br /><input type="submit" name="unzip_chunks"></form>
<u><b>UNZIP BY CHUNKS 2</u></b><br />*make sure zip file is inside public_html folder<br />
<form method="post" action="">FILE:<br /><input type="text" name="url" value="file.zip"><br /><input type="submit" name="unzip_chunks2"></form>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment