|
7 | 7 | use ZipArchive;
|
8 | 8 | use RecursiveIteratorIterator;
|
9 | 9 | use RecursiveDirectoryIterator;
|
| 10 | +use Tamedevelopers\Support\Tame; |
10 | 11 | use Tamedevelopers\Support\Traits\TameTrait;
|
11 | 12 |
|
12 | 13 | class Zip {
|
13 | 14 |
|
14 | 15 | use TameTrait;
|
15 | 16 |
|
16 |
| - /** |
17 |
| - * Unzip a file or folder. |
18 |
| - * |
19 |
| - * @param string $sourcePath |
20 |
| - * - [base path will be automatically added] |
21 |
| - * |
22 |
| - * @param string $destination |
23 |
| - * - [base path will be automatically added] |
24 |
| - * |
25 |
| - * @return bool |
26 |
| - */ |
27 |
| - static public function unzip($sourcePath, $destination) |
28 |
| - { |
29 |
| - $sourcePath = self::getBasePath($sourcePath); |
30 |
| - $destination = self::getBasePath($destination); |
31 |
| - |
32 |
| - // If it's a zip file, call the unzipFile function |
33 |
| - if (pathinfo($sourcePath, PATHINFO_EXTENSION) === 'zip') { |
34 |
| - return self::unzipFile($sourcePath, $destination); |
35 |
| - } |
36 |
| - |
37 |
| - // If it's a folder, call the unzipFolder function |
38 |
| - if (is_dir($sourcePath)) { |
39 |
| - return self::unzipFolder($sourcePath, $destination); |
40 |
| - } |
41 |
| - |
42 |
| - return false; // Unsupported file type |
43 |
| - } |
44 | 17 |
|
45 | 18 | /**
|
46 | 19 | * Zip a file or folder.
|
@@ -78,6 +51,62 @@ static public function zip($sourcePath, $destinationZip)
|
78 | 51 | return file_exists($destinationZip);
|
79 | 52 | }
|
80 | 53 |
|
| 54 | + /** |
| 55 | + * Unzip a file or folder. |
| 56 | + * |
| 57 | + * @param string $sourcePath |
| 58 | + * - [base path will be automatically added] |
| 59 | + * |
| 60 | + * @param string $destination |
| 61 | + * - [base path will be automatically added] |
| 62 | + * |
| 63 | + * @return bool |
| 64 | + */ |
| 65 | + static public function unzip($sourcePath, $destination) |
| 66 | + { |
| 67 | + $sourcePath = self::getBasePath($sourcePath); |
| 68 | + $destination = self::getBasePath($destination); |
| 69 | + |
| 70 | + // If it's a zip file, call the unzipFile function |
| 71 | + if (pathinfo($sourcePath, PATHINFO_EXTENSION) === 'zip') { |
| 72 | + return self::unzipFile($sourcePath, $destination); |
| 73 | + } |
| 74 | + |
| 75 | + // If it's a folder, call the unzipFolder function |
| 76 | + if (is_dir($sourcePath)) { |
| 77 | + return self::unzipFolder($sourcePath, $destination); |
| 78 | + } |
| 79 | + |
| 80 | + return false; // Unsupported file type |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * Download Zipped File |
| 85 | + * |
| 86 | + * @param string $fileName |
| 87 | + * @param bool $unlink |
| 88 | + * @return void |
| 89 | + */ |
| 90 | + static public function download($fileName, $unlink = true) |
| 91 | + { |
| 92 | + $zipfilePath = self::getBasePath($fileName); |
| 93 | + |
| 94 | + if(Tame::exists($zipfilePath)){ |
| 95 | + // Set headers to download the ZIP file |
| 96 | + header('Content-Type: application/zip'); |
| 97 | + header("Content-Disposition: attachment; filename={$fileName}"); |
| 98 | + header('Content-Length: ' . filesize($zipfilePath)); |
| 99 | + |
| 100 | + // Read the file to output the download |
| 101 | + readfile($zipfilePath); |
| 102 | + |
| 103 | + // Delete the ZIP file after download (optional) |
| 104 | + if($unlink){ |
| 105 | + unlink($zipfilePath); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
81 | 110 | /**
|
82 | 111 | * Unzip a zip file.
|
83 | 112 | *
|
|
0 commit comments