For unzipping stuff.
##TODO
- Add zip functionality
<?php | |
namespace App\Controller\Component; | |
use Cake\Controller\Component; | |
class ZipComponent extends Component | |
{ | |
public function remove_ext ($filename) | |
{ | |
$fnArray = explode('.', $filename); | |
unset($fnArray[count($fnArray) - 1]); | |
return implode('-', $fnArray); | |
} | |
public function mkdir ($dirr) | |
{ | |
if (!file_exists($dirr)) { | |
mkdir($dirr); | |
} | |
} | |
public function unzip_file ($file, $toFolder = false) | |
{ | |
$fullpath = ROOT . '/' . $file; | |
$zip = zip_open($fullpath); | |
if ($zip) | |
{ | |
if (!$toFolder) | |
{ | |
$toFolder = ROOT . '/' . $this->remove_ext($file); | |
} | |
else | |
{ | |
$toFolder = ROOT . '/' . $toFolder; | |
} | |
$this->mkdir($toFolder); | |
while ($zip_entry = zip_read($zip)) | |
{ | |
$zip_content = $toFolder.'/'.zip_entry_name($zip_entry); | |
if (substr($zip_content, strlen($zip_content) - 1, 1) == '/' && !file_exists($zip_content)) { | |
mkdir($zip_content); | |
} | |
else if (!file_exists($zip_content)) { | |
$fp = fopen($toFolder.'/'.zip_entry_name($zip_entry), "w"); | |
if (zip_entry_open($zip, $zip_entry, "r")) | |
{ | |
$buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); | |
fwrite($fp,"$buf"); | |
zip_entry_close($zip_entry); | |
fclose($fp); | |
} | |
} | |
} | |
zip_close($zip); | |
} | |
unlink($fullpath); | |
} | |
} |