Skip to content

Instantly share code, notes, and snippets.

@dewwwald
Created August 1, 2016 14:03
Show Gist options
  • Save dewwwald/4f4fa4da8848fa0bff5267e2d900b153 to your computer and use it in GitHub Desktop.
Save dewwwald/4f4fa4da8848fa0bff5267e2d900b153 to your computer and use it in GitHub Desktop.
Cake Zip Component

CakePHP ZipComponent

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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment