-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzipper.php
More file actions
76 lines (69 loc) · 2.19 KB
/
zipper.php
File metadata and controls
76 lines (69 loc) · 2.19 KB
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
<?php
class HZip
{
/**
* Add files and sub-directories in a folder to zip file.
* @param string $folder
* @param ZipArchive $zipFile
* @param int $exclusiveLength Number of text to be exclusived from the file path.
*/
private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir($folder);
while (false !== $f = readdir($handle)) {
if ($f != '.' && $f != '..') {
$filePath = "$folder/$f";
// Remove prefix from file path before add to zip.
$localPath = substr($filePath, $exclusiveLength);
if (is_file($filePath)) {
$zipFile->addFile($filePath, $localPath);
} elseif (is_dir($filePath)) {
// Add sub-directory.
$zipFile->addEmptyDir($localPath);
self::folderToZip($filePath, $zipFile, $exclusiveLength);
}
}
}
closedir($handle);
}
/**
* Zip a folder (include itself).
* Usage:
* HZip::zipDir('/path/to/sourceDir', '/path/to/out.zip');
*
* @param string $sourcePath Path of directory to be zip.
* @param string $outZipPath Path of output zip file.
*/
public static function zipDir($sourcePath, $outZipPath)
{
$pathInfo = pathInfo($sourcePath);
$parentPath = $pathInfo['dirname'];
$dirName = $pathInfo['basename'];
$z = new ZipArchive();
$z->open($outZipPath, ZIPARCHIVE::CREATE);
$z->addEmptyDir($dirName);
self::folderToZip($sourcePath, $z, strlen("$parentPath/"));
$z->close();
}
}
// Real Path & Language
$dir = $_GET['path'];
$lang = $_GET['name'];
// $filename = $lang . '.zip';
// $full = $dir . '/' . $filename;
$date = date("YmdHis").substr((string)microtime(), 1, 6);
$filename = $lang . '.zip';
$filename_temp = $lang . '_' . $date . '.zip';
// Zip!
HZip::zipDir($dir, $filename_temp);
// Download
header("Pragma: public");
header('Content-Type: application/zip');
header("Content-Disposition: attachment; filename=$filename");
header("Content-Transfer-Encoding: binary");
header('Content-Length: ' . filesize($filename_temp));
// header("Location: $lang.zip");
readFile($filename_temp);
// Delete file
@unlink($filename);
@unlink($filename_temp);
?>