Gentics Portal.Node PHP API
 All Classes Namespaces Functions Variables Pages
Static Public Member Functions | List of all members
ZipHelper Class Reference

Static Public Member Functions

static compress ($sources, $destination)
static extract ($zipPath, $dest, $merge=true)

Detailed Description

Class for compressing and extracting set of folders/files in zip format

Definition at line 5 of file ZipHelper.php.

Member Function Documentation

static ZipHelper::compress (   $sources,
  $destination 
)
static

Recursively zip folder

Parameters
array$sourcesarray of sources. String key means source contains string of content. Otherwise source contains name of file
string$destinationzip archive destination folder
Exceptions
Exception
Returns
bool

Definition at line 17 of file ZipHelper.php.

Referenced by Release\backupProject(), and Release\make().

{
if (!extension_loaded('zip')) {
throw new Exception('Php Zip extension not loaded');
}
if (empty($sources)) {
return false;
}
if (is_string($sources)) {
$sources = array($sources);
}
$zip = new ZipArchive();
if (!$zip->open($destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE | ZIPARCHIVE::CHECKCONS)) {
return false;
}
foreach ($sources as $name => $source) {
if (is_int($name)) {
$exclude = array();
$sourcePath = $source;
if (is_array($source)) {
$sourcePath = $source['path'];
if (isset($source['exclude'])) {
$exclude = $source['exclude'];
}
}
$sourcePath = str_replace('\\', '/', realpath($sourcePath));
if (is_dir($sourcePath) === true) {
$iterator = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourcePath, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($iterator as $file) {
if ($file->isLink()) {
continue;
}
//for windows systems
$file = str_replace('\\', '/', realpath($file));
$zipPath = str_replace($sourcePath . '/', '', $file);
//if in $exclude skip
$skip = count(
array_filter(
$exclude,
function($ex) use ($zipPath)
{
return strpos($zipPath, $ex) === 0;
}
)
) > 0;
if ($skip) {
continue;
}
//add to archive
if (is_dir($file) === true) {
$zip->addEmptyDir($zipPath . '/');
} else if (is_file($file) === true) {
$zip->addFromString($zipPath, file_get_contents($file));
}
}
} else if (is_file($sourcePath) === true) {
$zip->addFromString(basename($sourcePath), file_get_contents($sourcePath));
}
} else {
$zip->addFromString($name, $source);
}
}
return $zip->close();
}
static ZipHelper::extract (   $zipPath,
  $dest,
  $merge = true 
)
static

Extract zip archive to folder

Parameters
string$zipPathpath to zip archive
string$destdestination folder
bool$mergeif false replace destination folder by new folders from archive, otherwise merge folders
Exceptions
Exception
Returns
bool true if extraction success, false otherwise

Definition at line 95 of file ZipHelper.php.

References FileHelper\rmdirr().

Referenced by Release\update().

{
if (!extension_loaded('zip')) {
throw new Exception('Php Zip extension not loaded');
}
$zipH = zip_open($zipPath);
if (!$zipH) {
throw new Exception("Can't open archive $zipPath");
}
if (file_exists($dest) && !is_writable($dest)) {
throw new Exception("Destination directory $dest is not writable");
} elseif (!is_writable($dest) && !mkdir($dest)) {
throw new Exception("Can't create directory in $dest");
}
while ($entry = zip_read($zipH)) {
$entryPath = zip_entry_name($entry);
$destPath = $dest . '/' . $entryPath;
if (substr($entryPath, -1) == '/') {
if (file_exists($destPath)) {
if (!$merge) {
FileHelper::rmdirr($destPath);
mkdir($destPath);
}
} else {
mkdir($destPath);
}
} else {
if (!is_dir($dir = dirname($destPath))) {
mkdir($dir, 0777, true);
}
if (@file_put_contents($destPath, zip_entry_read($entry, zip_entry_filesize($entry))) === false) {
throw new Exception("$destPath is not writable");
}
}
}
}

The documentation for this class was generated from the following file: