Skip to content

Commit

Permalink
adding zip helper
Browse files Browse the repository at this point in the history
  • Loading branch information
tim-kos committed Nov 24, 2011
1 parent 6bb53a9 commit f2fafd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
Empty file removed README
Empty file.
7 changes: 7 additions & 0 deletions Readme.md
@@ -0,0 +1,7 @@
# Usage

// do not overwrite if file exists
$this->Zip->create($pathToZipFile, $arrayOfFileLocations, false);

// overwrite if file exists
$this->Zip->create($pathToZipFile, $arrayOfFileLocations, true);
35 changes: 35 additions & 0 deletions zip.php
@@ -0,0 +1,35 @@
<?php
class ZipHelper extends AppHelper {
public function create($destination = '', $files = array(), $overwrite = false) {
if (file_exists($destination) && !$overwrite) {
return false;
}

$validFiles = array();
if (is_array($files)) {
foreach ($files as $file) {
if (file_exists($file)) {
$validFiles[] = $file;
}
}
}

if (count($validFiles) < 1) {
return false;
}

$zip = new ZipArchive();
$type = $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE;
if ($zip->open($destination, $type) !== true) {
return false;
}

$dest = str_replace('.zip', '', basename($destination));
foreach ($validFiles as $file) {
$zip->addFile($file, $dest . DS . basename($file));
}
$zip->close();

return file_exists($destination);
}
}

0 comments on commit f2fafd7

Please sign in to comment.