Skip to content

Commit c5b7f93

Browse files
update
1 parent 02b4cd6 commit c5b7f93

File tree

2 files changed

+58
-28
lines changed

2 files changed

+58
-28
lines changed

Tests/zip.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
// TameZip()->zip('tests', 'newData.zip');
1313
// TameZip()->unzip('newData.zip', '/');
14+
// TameZip()->download('newData.zip');
1415

1516
dd(
1617

Zip.php

Lines changed: 57 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,40 +7,13 @@
77
use ZipArchive;
88
use RecursiveIteratorIterator;
99
use RecursiveDirectoryIterator;
10+
use Tamedevelopers\Support\Tame;
1011
use Tamedevelopers\Support\Traits\TameTrait;
1112

1213
class Zip {
1314

1415
use TameTrait;
1516

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-
}
4417

4518
/**
4619
* Zip a file or folder.
@@ -78,6 +51,62 @@ static public function zip($sourcePath, $destinationZip)
7851
return file_exists($destinationZip);
7952
}
8053

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+
81110
/**
82111
* Unzip a zip file.
83112
*

0 commit comments

Comments
 (0)