Skip to content

Instantly share code, notes, and snippets.

@MilkZoft
Created January 19, 2012 03:12
Show Gist options
  • Select an option

  • Save MilkZoft/1637489 to your computer and use it in GitHub Desktop.

Select an option

Save MilkZoft/1637489 to your computer and use it in GitHub Desktop.
codejobs - Create a Zip file - PHP
<?php
function createZip($files = array(), $destination = NULL, $overwrite = FALSE) {
if(file_exists($destination) and !$overwrite) {
return FALSE;
}
$validFiles = array();
if(is_array($files)) {
foreach($files as $file) {
if(file_exists($file)) {
$validFiles[] = $file;
}
}
}
if(count($validFiles)) {
$Zip = new ZipArchive();
if(!$Zip->open($destination, $overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE)) {
return FALSE;
}
foreach($validFiles as $file) {
$Zip->addFile($file, $file);
}
$Zip->close();
return file_exists($destination);
} else {
return false;
}
}
function unzipFile($file, $destination) {
$Zip = new ZipArchive();
if(!$Zip->open($file)) {
die("Could not open archive");
}
$Zip->extractTo($destination);
$Zip->close();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment