Last active
September 16, 2018 17:38
-
-
Save pateketrueke/37f3cdf939d636d5aa4848c4d0d4d0bb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$start = memory_get_usage(true); | |
define('INPUT_FILE', $argv[1]); | |
define('IS_DECODE', array_search('--decode', $argv) !== false); | |
if (!is_file(INPUT_FILE)) { | |
echo "Invalid or missing input.\n"; | |
echo "Usage: php extract-xml.php download.xml [--decode]\n"; | |
echo "Examples:\n"; | |
echo " php extract-xml.php download.xml --decode ; unzip download.xml.zip\n"; | |
echo " php extract-xml.php download.xml ; base64 --decode download.xml.base64 > download.final.zip\n"; | |
exit(1); | |
} | |
$f = fopen(INPUT_FILE, 'r'); | |
$h = fread($f, 1024); | |
$i = strpos($h, '<Paquete>'); | |
$o = fopen(INPUT_FILE . (IS_DECODE ? '.zip' : '.base64'), 'w'); | |
$b = ''; | |
function clean($value) { | |
return trim(preg_replace('/ +/', '', $value)); | |
} | |
if ($i !== false) { | |
if (IS_DECODE) { | |
$b .= clean(substr($h, $i + 9)); | |
} else { | |
fwrite($o, clean(substr($h, $i + 9))); | |
} | |
} | |
while (1) { | |
$c = fread($f, 4096); | |
$j = strpos($c, '</Paquete>'); | |
if ($j !== false) { | |
if (IS_DECODE) { | |
$b .= clean(substr($c, 0, $j)); | |
} else { | |
fwrite($o, clean(substr($c, 0, $j))); | |
} | |
break; | |
} | |
if (IS_DECODE) { | |
$b .= clean($c); | |
} else { | |
fwrite($o, clean($c)); | |
} | |
$x++; | |
} | |
if (IS_DECODE) { | |
fwrite($o, base64_decode($b)); | |
} | |
fclose($f); | |
fclose($o); | |
$end = memory_get_usage(true); | |
$size = $end - $start; | |
$unit = ['B', 'KB', 'MB', 'GB', 'TB', 'PB']; | |
if ($size) { | |
echo @round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . ' ' . $unit[$i]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment