Permalink
Browse files

adding zip helper

  • Loading branch information...
1 parent 6bb53a9 commit f2fafd746d5326e0efd70118863c70c7f9b1a521 @tim-kos committed Nov 24, 2011
Showing with 42 additions and 0 deletions.
  1. 0 README
  2. +7 −0 Readme.md
  3. +35 −0 zip.php
View
No changes.
View
@@ -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);
View
@@ -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.