Last active
October 29, 2020 06:21
-
-
Save ybagheri/f23a3d199cac6023a66ee37c5dd2e1a8 to your computer and use it in GitHub Desktop.
zip unzip php
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 | |
$zipfile= __DIR__.'/bot.zip'; | |
Zip(__DIR__.'/bot/',$zipfile); | |
unzip($zipfile, __DIR__.'/a'); | |
function Zip($source, $destination) | |
{ | |
if (!extension_loaded('zip') || !file_exists($source)) { | |
echo PHP_EOL.'here false'.PHP_EOL; | |
return false; | |
} | |
$zip = new ZipArchive(); | |
if (!$zip->open($destination, ZIPARCHIVE::CREATE)) { | |
return false; | |
} | |
echo PHP_EOL.'here'.PHP_EOL; | |
$source = str_replace('\\', '/', realpath($source)); | |
if (is_dir($source) === true) | |
{ | |
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($source), RecursiveIteratorIterator::SELF_FIRST); | |
foreach ($files as $file) | |
{ | |
$file = str_replace('\\', '/', $file); | |
// Ignore "." and ".." folders | |
if( in_array(substr($file, strrpos($file, '/')+1), array('.', '..')) ) | |
continue; | |
$file = realpath($file); | |
if (is_dir($file) === true) | |
{ | |
$zip->addEmptyDir(str_replace($source . '/', '', $file . '/')); | |
} | |
else if (is_file($file) === true) | |
{ | |
$zip->addFromString(str_replace($source . '/', '', $file), file_get_contents($file)); | |
} | |
} | |
} | |
else if (is_file($source) === true) | |
{ | |
$zip->addFromString(basename($source), file_get_contents($source)); | |
} | |
return $zip->close(); | |
} | |
function unzip($source, $destination) | |
{ | |
$zip = new ZipArchive; | |
$res = $zip->open($source); | |
if ($res === TRUE) { | |
$zip->extractTo($destination); | |
$zip->close(); | |
echo 'ok'; | |
} else { | |
echo 'failed'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment