-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathZip.php
216 lines (173 loc) · 6.32 KB
/
Zip.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
<?php
declare(strict_types=1);
namespace Tamedevelopers\Support;
use ZipArchive;
use RecursiveIteratorIterator;
use RecursiveDirectoryIterator;
use Tamedevelopers\Support\Tame;
use Tamedevelopers\Support\Traits\TameTrait;
class Zip {
use TameTrait;
/**
* Zip a file or folder.
*
* @param string $sourcePath The path to the file or folder to zip.
* - [base path will be automatically added]
*
* @param string $destinationZip The path for the resulting zip file.
* - [base path will be automatically added]
*
* @return bool True if the zip operation was successful, false otherwise.
*/
static public function zip($sourcePath, $destinationZip)
{
$sourcePath = self::getBasePath($sourcePath);
$destinationZip = self::getBasePath($destinationZip);
// If it's a folder, call the zipFolder function
if (is_dir($sourcePath)) {
return self::zipFolder($sourcePath, $destinationZip);
}
// If it's a file, create a zip containing just that file
$zip = new ZipArchive();
if ($zip->open($destinationZip, ZipArchive::CREATE) !== true) {
return false;
}
// Add the file to the zip
$zip->addFile($sourcePath, basename($sourcePath));
$zip->close();
return file_exists($destinationZip);
}
/**
* Unzip a file or folder.
*
* @param string $sourcePath
* - [base path will be automatically added]
*
* @param string $destination
* - [base path will be automatically added]
*
* @return bool
*/
static public function unzip($sourcePath, $destination)
{
$sourcePath = self::getBasePath($sourcePath);
$destination = self::getBasePath($destination);
// If it's a zip file, call the unzipFile function
if (pathinfo($sourcePath, PATHINFO_EXTENSION) === 'zip') {
return self::unzipFile($sourcePath, $destination);
}
// If it's a folder, call the unzipFolder function
if (is_dir($sourcePath)) {
return self::unzipFolder($sourcePath, $destination);
}
return false; // Unsupported file type
}
/**
* Download Zipped File
*
* @param string $fileName
* @param bool $unlink
* @return void
*/
static public function download($fileName, $unlink = true)
{
$zipfilePath = self::getBasePath($fileName);
if(Tame::exists($zipfilePath)){
// Set headers to download the ZIP file
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename={$fileName}");
header('Content-Length: ' . filesize($zipfilePath));
// Read the file to output the download
readfile($zipfilePath);
// Delete the ZIP file after download (optional)
if($unlink){
unlink($zipfilePath);
}
}
}
/**
* Unzip a zip file.
*
* @param string $file The path to the zip file.
* @param string $destination The path to the destination directory where the contents will be extracted.
* @return bool True if the unzip operation was successful, false otherwise.
*/
static private function unzipFile($file, $destination)
{
// Create object
$zip = new ZipArchive();
// Open archive
if ($zip->open($file) !== true) {
return false;
}
// Extract contents to destination directory
$zip->extractTo($destination);
// Close archive
$zip->close();
return true;
}
/**
* Zip a folder and its contents.
*
* @param string $sourceFolder The path to the folder to zip.
* @param string $destinationZip The path for the resulting zip file.
* @return bool True if the zip operation was successful, false otherwise.
*/
static private function zipFolder($sourceFolder, $destinationZip)
{
$zip = new ZipArchive();
if ($zip->open($destinationZip, ZipArchive::CREATE) !== true) {
return false;
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourceFolder),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
// Remove the source folder name from the local path
$localPath = substr($filePath, strlen($sourceFolder) + 1);
// Add the contents of the source folder to the zip without the source folder name
$zip->addFile($filePath, $localPath);
}
}
$zip->close();
return file_exists($destinationZip);
}
/**
* Unzip a folder and its contents.
*
* @param string $sourceFolder The path to the folder to unzip.
* @param string $destination The path to the destination directory where the contents will be extracted.
* @return bool True if the unzip operation was successful, false otherwise.
*/
static private function unzipFolder($sourceFolder, $destination)
{
// Ensure the destination directory exists
if (!is_dir($destination)) {
mkdir($destination, 0777, true);
}
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($sourceFolder),
RecursiveIteratorIterator::LEAVES_ONLY
);
foreach ($files as $name => $file) {
if (!$file->isDir()) {
$filePath = $file->getRealPath();
// Remove the source folder name from the local path
$localPath = substr($filePath, strlen($sourceFolder) + 1);
// full path
$destinationPath = $destination . '/' . $localPath;
// Ensure the destination directory for the file exists
$destinationDir = dirname($destinationPath);
if (!is_dir($destinationDir)) {
mkdir($destinationDir, 0777, true);
}
// copy the contents of the filepath to destinationPath
copy($filePath, $destinationPath);
}
}
return true;
}
}