1: <?php
2: 3: 4:
5: class QArchive {
6: private static $strLastError;
7:
8: 9: 10: 11:
12: public static function getLastError() {
13: return self::$strLastError;
14: }
15:
16: 17: 18: 19: 20: 21:
22: public static function extractZip($archive, $destination) {
23: if (!function_exists('zip_open')) {
24: throw new Exception("ZIP extension is not enabled on this installation of PHP. Recompile your installation of PHP with --enable-zip parameter.");
25: }
26:
27:
28:
29: $zip = zip_open($archive);
30: if (is_resource($zip)) {
31:
32: if (!mkdir($destination)) {
33: self::$strLastError = "Unable to create extraction destination folder " . $destination;
34: return false;
35: }
36:
37:
38: $createdFolders = array();
39: while ($file = zip_read($zip)) {
40: if(is_resource($file)) {
41: if (zip_entry_open($zip, $file, "r")) {
42: if (substr(zip_entry_name($file), strlen(zip_entry_name($file)) - 1) != "/") {
43:
44:
45: $folderStack = explode("/", zip_entry_name($file));
46: if (sizeof($folderStack) > 1) {
47: for ($i = 0; $i < sizeof($folderStack) - 1; $i++) {
48: $arraySubsection = array_slice($folderStack, 0, $i + 1);
49: $item = implode("/", $arraySubsection);
50:
51: if (!in_array($item, $createdFolders)) {
52:
53: $createdFolders[] = $item;
54: mkdir($destination . $item);
55: }
56: }
57: }
58:
59: $strSectionToAppend = zip_entry_read($file, zip_entry_filesize($file));
60: $strSavePath = $destination . zip_entry_name($file);
61:
62: QFile::writeFile($strSavePath, $strSectionToAppend);
63:
64: zip_entry_close($file);
65: }
66: } else {
67: self::$strLastError = "Unable to read zip entry";
68: return false;
69: }
70: } else {
71: self::$strLastError = self::zipFileErrMsg($file);
72: return false;
73: }
74: }
75: zip_close($zip);
76: } else {
77: self::$strLastError = self::zipFileErrMsg($zip);
78: return false;
79: }
80: return true;
81: }
82:
83: 84: 85: 86: 87: 88: 89:
90: private static function zipFileErrMsg($errno) {
91:
92: $zipFileFunctionsErrors = array(
93: 'ZIPARCHIVE::ER_MULTIDISK' => 'Multi-disk zip archives not supported.',
94: 'ZIPARCHIVE::ER_RENAME' => 'Renaming temporary file failed.',
95: 'ZIPARCHIVE::ER_CLOSE' => 'Closing zip archive failed',
96: 'ZIPARCHIVE::ER_SEEK' => 'Seek error',
97: 'ZIPARCHIVE::ER_READ' => 'Read error',
98: 'ZIPARCHIVE::ER_WRITE' => 'Write error',
99: 'ZIPARCHIVE::ER_CRC' => 'CRC error',
100: 'ZIPARCHIVE::ER_ZIPCLOSED' => 'Containing zip archive was closed',
101: 'ZIPARCHIVE::ER_NOENT' => 'No such file.',
102: 'ZIPARCHIVE::ER_EXISTS' => 'File already exists',
103: 'ZIPARCHIVE::ER_OPEN' => 'Can\'t open file',
104: 'ZIPARCHIVE::ER_TMPOPEN' => 'Failure to create temporary file.',
105: 'ZIPARCHIVE::ER_ZLIB' => 'Zlib error',
106: 'ZIPARCHIVE::ER_MEMORY' => 'Memory allocation failure',
107: 'ZIPARCHIVE::ER_CHANGED' => 'Entry has been changed',
108: 'ZIPARCHIVE::ER_COMPNOTSUPP' => 'Compression method not supported.',
109: 'ZIPARCHIVE::ER_EOF' => 'Premature EOF',
110: 'ZIPARCHIVE::ER_INVAL' => 'Invalid argument',
111: 'ZIPARCHIVE::ER_NOZIP' => 'Not a zip archive',
112: 'ZIPARCHIVE::ER_INTERNAL' => 'Internal error',
113: 'ZIPARCHIVE::ER_INCONS' => 'Zip archive inconsistent',
114: 'ZIPARCHIVE::ER_REMOVE' => 'Can\'t remove file',
115: 'ZIPARCHIVE::ER_DELETED' => 'Entry has been deleted');
116:
117: foreach ($zipFileFunctionsErrors as $constName => $errorMessage) {
118: if (defined($constName) and constant($constName) === $errno) {
119: return 'Zip File error: ' . $errorMessage;
120: }
121: }
122: return 'Zip File Function error: unknown';
123: }
124: }