1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: 19: 20: 21: 22:
23: class cZipArchive {
24:
25: 26: 27: 28: 29: 30: 31: 32: 33:
34: public static function readExistingFiles($dirPath) {
35:
36:
37: if (!is_dir($dirPath)) {
38: return array();
39: }
40:
41:
42: if (false === ($handle = cDirHandler::read($dirPath))) {
43: return array();
44: }
45:
46: $array = array();
47: foreach ($handle as $file) {
48: if (cFileHandler::fileNameBeginsWithDot($file)) {
49:
50:
51: continue;
52: } else if (!cFileHandler::validateFilename($file, false)) {
53:
54:
55: continue;
56: } else {
57: $array[] = $file;
58: }
59: }
60:
61:
62: return $array;
63: }
64:
65: 66: 67: 68: 69: 70:
71: public static function isExtracted($dirPath) {
72: if (!file_exists($dirPath)) {
73: return false;
74: } else if (!is_dir($dirPath)) {
75: return false;
76: } else {
77: return true;
78: }
79: }
80:
81: 82: 83: 84: 85: 86: 87: 88:
89: public static function extractOverRide($file, $extractPath, $extractPathUserInput = NULL) {
90:
91:
92: if (isset($extractPathUserInput)) {
93: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
94: }
95:
96: $zip = new ZipArchive();
97:
98:
99: if (!$zip->open($file)) {
100: echo ('can not open zip file!');
101: return;
102: }
103:
104: for ($i = 0; $i < $zip->numFiles; $i++) {
105: $file = $zip->getNameIndex($i);
106:
107: $tmpFile = str_replace('/', '', $file);
108:
109: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
110: $zip->extractTo($extractPath, $file);
111: }
112: }
113:
114: $zip->close();
115: }
116:
117: 118: 119: 120: 121: 122: 123:
124: public static function extract($file, $extractPath, $extractPathUserInput = NULL) {
125: if (isset($extractPathUserInput)) {
126:
127:
128: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
129: }
130:
131: if (file_exists($extractPath) and is_dir($extractPath)) {
132: $ar = cZipArchive::readExistingFiles($extractPath);
133: }
134: $zip = new ZipArchive();
135:
136:
137: if (!$zip->open($file)) {
138: echo ('can not open zip file!');
139: return;
140: }
141:
142:
143: if (cZipArchive::isExtracted($extractPath)) {
144: for ($i = 0; $i < $zip->numFiles; $i++) {
145: $file = $zip->getNameIndex($i);
146: $tmpFile = str_replace('/', '', $file);
147: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
148: if (!file_exists($extractPath . '/' . $file)) {
149: $zip->extractTo($extractPath, $file);
150: }
151: }
152: }
153: } else {
154: for ($i = 0; $i < $zip->numFiles; $i++) {
155: $file = $zip->getNameIndex($i);
156:
157: $tmpFile = str_replace('/', '', $file);
158: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
159: $zip->extractTo($extractPath, $file);
160: }
161: }
162: }
163: $zip->close();
164: }
165:
166: 167: 168: 169: 170: 171: 172:
173: public static function createZip($zipFilePath, $dirPath, array $filePathes) {
174: $zip = new ZipArchive();
175: if ($zip->open($dirPath . $zipFilePath, ZipArchive::CREATE) == TRUE) {
176: foreach ($filePathes as $key => $file) {
177: $zip->addFile($dirPath . $file, $file);
178: }
179: $zip->close();
180: }
181: }
182: }
183: