1: <?php
2: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
3: class cZipArchive {
4:
5: public static function readExistingFiles($dirPath) {
6: $ar = array();
7: if (is_dir($dirPath)) {
8: if ($handle = opendir($dirPath)) {
9:
10: while (false !== ($file = readdir($handle))) {
11:
12:
13:
14: if (cFileHandler::validateFilename($file, FALSE) && $file[0] != '.') {
15:
16: $ar[] = $file;
17: }
18: }
19:
20: closedir($handle);
21: }
22: }
23: return $ar;
24: }
25:
26: public static function ($pathDir) {
27: if (file_exists($pathDir) and is_dir($pathDir)) {
28: return true;
29: } else {
30: return false;
31: }
32: }
33:
34: public static function ($file, $extractPath, $extractPathUserInput = NULL) {
35: if (isset($extractPathUserInput)) {
36:
37:
38: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
39: }
40:
41: $zip = new ZipArchive();
42: $state = $zip->open($file);
43:
44: if ($state == TRUE) {
45:
46: for ($i = 0; $i < $zip->numFiles; $i++) {
47:
48: $file = $zip->getNameIndex($i);
49:
50:
51: $tmpFile = str_replace('/', '', $file);
52:
53: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
54: $zip->extractTo($extractPath, $file);
55: }
56: }
57:
58: $zip->close();
59: } else {
60: echo ('can not open zip file!');
61: }
62: }
63:
64: public static function extract($file, $extractPath, $extractPathUserInput = NULL) {
65: if (isset($extractPathUserInput)) {
66:
67:
68: $extractPath .= uplCreateFriendlyName($extractPathUserInput);
69: }
70:
71: if (file_exists($extractPath) and is_dir($extractPath)) {
72: $ar = cZipArchive::readExistingFiles($extractPath);
73: }
74:
75:
76: $zip = new ZipArchive();
77: $state = $zip->open($file);
78:
79:
80: if (cZipArchive::isExtracted($extractPath)) {
81:
82: if ($state == TRUE) {
83:
84: for ($i = 0; $i < $zip->numFiles; $i++) {
85:
86: $file = $zip->getNameIndex($i);
87:
88: $tmpFile = str_replace('/', '', $file);
89: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
90:
91: if (!file_exists($extractPath . '/' . $file)) {
92:
93: $zip->extractTo($extractPath, $file);
94: }
95: }
96: }
97:
98: $zip->close();
99: } else {
100: echo ('can not open zip file!');
101: }
102: } else {
103: if ($state == TRUE) {
104:
105: for ($i = 0; $i < $zip->numFiles; $i++) {
106:
107: $file = $zip->getNameIndex($i);
108:
109: $tmpFile = str_replace('/', '', $file);
110:
111: if (cFileHandler::validateFilename($tmpFile, FALSE) && (substr($tmpFile, 0, 1) != '.') && (substr($tmpFile, 0, 1) != '_')) {
112: $zip->extractTo($extractPath, $file);
113: }
114: }
115: $zip->close();
116: } else {
117: echo ('can not open zip file!');
118: }
119: }
120: }
121:
122: public static function createZip($zipFilePath, $dirPath, array $filePathes) {
123: $zip = new ZipArchive();
124: if ($zip->open($dirPath . $zipFilePath, ZipArchive::CREATE) == TRUE) {
125: foreach ($filePathes as $key => $file) {
126: $zip->addFile($dirPath . $file, $file);
127: }
128: $zip->close();
129: }
130: }
131:
132: }
133:
134: ?>
135: