1<?php 2///////////////////////////////////////////////////////////////// 3/// getID3() by James Heinrich <info@getid3.org> // 4// available at https://github.com/JamesHeinrich/getID3 // 5// or https://www.getid3.org // 6// or http://getid3.sourceforge.net // 7// // 8// /demo/demo.zip.php - part of getID3() // 9// Sample script how to use getID3() to decompress zip files // 10// see readme.txt for more details // 11// /// 12///////////////////////////////////////////////////////////////// 13 14 15function UnzipFileContents($filename, &$errors) { 16 $errors = array(); 17 $DecompressedFileContents = array(); 18 if (!class_exists('getID3')) { 19 $errors[] = 'class getID3 not defined, please include getid3.php'; 20 } elseif (include_once('module.archive.zip.php')) { 21 $getid3 = new getID3(); 22 $getid3->info['filesize'] = filesize($filename); 23 ob_start(); 24 if ($getid3->fp = fopen($filename, 'rb')) { 25 ob_end_clean(); 26 $getid3_zip = new getid3_zip($getid3); 27 $getid3_zip->analyze(); 28 if (($getid3->info['fileformat'] == 'zip') && !empty($getid3->info['zip']['files'])) { 29 if (!empty($getid3->info['zip']['central_directory'])) { 30 $ZipDirectoryToWalk = $getid3->info['zip']['central_directory']; 31 } elseif (!empty($getid3->info['zip']['entries'])) { 32 $ZipDirectoryToWalk = $getid3->info['zip']['entries']; 33 } else { 34 $errors[] = 'failed to parse ZIP attachment "'.$filename.'" (no central directory)<br>'; 35 fclose($getid3->fp); 36 return false; 37 } 38 foreach ($ZipDirectoryToWalk as $key => $valuearray) { 39 fseek($getid3->fp, $valuearray['entry_offset'], SEEK_SET); 40 $LocalFileHeader = $getid3_zip->ZIPparseLocalFileHeader(); 41 if ($LocalFileHeader['flags']['encrypted']) { 42 // password-protected 43 $DecompressedFileContents[$valuearray['filename']] = ''; 44 } else { 45 fseek($getid3->fp, $LocalFileHeader['data_offset'], SEEK_SET); 46 $compressedFileData = ''; 47 while ((strlen($compressedFileData) < $LocalFileHeader['compressed_size']) && !feof($getid3->fp)) { 48 $compressedFileData .= fread($getid3->fp, 32768); 49 } 50 switch ($LocalFileHeader['raw']['compression_method']) { 51 case 0: // store - great, just copy data unchanged 52 $uncompressedFileData = $compressedFileData; 53 break; 54 55 case 8: // deflate 56 ob_start(); 57 $uncompressedFileData = gzinflate($compressedFileData); 58 $gzinflate_errors = trim(strip_tags(ob_get_contents())); 59 ob_end_clean(); 60 if ($gzinflate_errors) { 61 $errors[] = 'gzinflate() failed for file ['.$LocalFileHeader['filename'].']: "'.$gzinflate_errors.'"'; 62 continue 2; 63 } 64 break; 65 66 case 1: // shrink 67 case 2: // reduce-1 68 case 3: // reduce-2 69 case 4: // reduce-3 70 case 5: // reduce-4 71 case 6: // implode 72 case 7: // tokenize 73 case 9: // deflate64 74 case 10: // PKWARE Date Compression Library Imploding 75 $DecompressedFileContents[$valuearray['filename']] = ''; 76 $errors[] = 'unsupported ZIP compression method ('.$LocalFileHeader['raw']['compression_method'].' = '.$getid3_zip->ZIPcompressionMethodLookup($LocalFileHeader['raw']['compression_method']).')'; 77 continue 2; 78 79 default: 80 $DecompressedFileContents[$valuearray['filename']] = ''; 81 $errors[] = 'unknown ZIP compression method ('.$LocalFileHeader['raw']['compression_method'].')'; 82 continue 2; 83 } 84 $DecompressedFileContents[$valuearray['filename']] = $uncompressedFileData; 85 unset($compressedFileData); 86 } 87 } 88 } else { 89 $errors[] = $filename.' does not appear to be a zip file'; 90 } 91 } else { 92 $error_message = ob_get_contents(); 93 ob_end_clean(); 94 $errors[] = 'failed to fopen('.$filename.', rb): '.$error_message; 95 } 96 } else { 97 $errors[] = 'failed to include_once(module.archive.zip.php)'; 98 } 99 return $DecompressedFileContents; 100} 101