17 public static function compress($sources, $destination)
19 if (!extension_loaded(
'zip')) {
20 throw new Exception(
'Php Zip extension not loaded');
22 if (empty($sources)) {
25 if (is_string($sources)) {
26 $sources = array($sources);
28 $zip =
new ZipArchive();
29 if (!$zip->open($destination, ZIPARCHIVE::CREATE | ZIPARCHIVE::OVERWRITE | ZIPARCHIVE::CHECKCONS)) {
32 foreach ($sources as $name => $source) {
35 $sourcePath = $source;
36 if (is_array($source)) {
37 $sourcePath = $source[
'path'];
38 if (isset($source[
'exclude'])) {
39 $exclude = $source[
'exclude'];
42 $sourcePath = str_replace(
'\\',
'/', realpath($sourcePath));
43 if (is_dir($sourcePath) ===
true) {
44 $iterator =
new RecursiveIteratorIterator(
45 new RecursiveDirectoryIterator($sourcePath, RecursiveDirectoryIterator::SKIP_DOTS),
46 RecursiveIteratorIterator::SELF_FIRST
48 foreach ($iterator as $file) {
49 if ($file->isLink()) {
53 $file = str_replace(
'\\',
'/', realpath($file));
54 $zipPath = str_replace($sourcePath .
'/',
'', $file);
59 function($ex) use ($zipPath)
61 return strpos($zipPath, $ex) === 0;
69 if (is_dir($file) ===
true) {
70 $zip->addEmptyDir($zipPath .
'/');
71 }
else if (is_file($file) ===
true) {
72 $zip->addFromString($zipPath, file_get_contents($file));
75 }
else if (is_file($sourcePath) ===
true) {
76 $zip->addFromString(basename($sourcePath), file_get_contents($sourcePath));
79 $zip->addFromString($name, $source);
95 public static function extract($zipPath, $dest, $merge =
true)
97 if (!extension_loaded(
'zip')) {
98 throw new Exception(
'Php Zip extension not loaded');
100 $zipH = zip_open($zipPath);
102 throw new Exception(
"Can't open archive $zipPath");
104 if (file_exists($dest) && !is_writable($dest)) {
105 throw new Exception(
"Destination directory $dest is not writable");
106 } elseif (!is_writable($dest) && !mkdir($dest)) {
107 throw new Exception(
"Can't create directory in $dest");
110 while ($entry = zip_read($zipH)) {
111 $entryPath = zip_entry_name($entry);
112 $destPath = $dest .
'/' . $entryPath;
113 if (substr($entryPath, -1) ==
'/') {
114 if (file_exists($destPath)) {
123 if (!is_dir($dir = dirname($destPath))) {
124 mkdir($dir, 0777,
true);
126 if (@file_put_contents($destPath, zip_entry_read($entry, zip_entry_filesize($entry))) ===
false) {
127 throw new Exception(
"$destPath is not writable");