8
8
* @author Andreas Tasch, at[tec], attec.at
9
9
* @license GNU GPL v3
10
10
* @package attec.toolbox
11
- * @version 0.1.0
11
+ * @version 0.1.1
12
12
*/
13
- define ('VERSION ' , '0.1.0 ' );
13
+ define ('VERSION ' , '0.1.1 ' );
14
14
15
15
$ timestart = microtime (TRUE );
16
16
$ GLOBALS ['status ' ] = array ();
17
17
18
18
$ unzipper = new Unzipper ;
19
19
if (isset ($ _POST ['dounzip ' ])) {
20
- //check if an archive was selected for unzipping
20
+ // Check if an archive was selected for unzipping.
21
21
$ archive = isset ($ _POST ['zipfile ' ]) ? strip_tags ($ _POST ['zipfile ' ]) : '' ;
22
22
$ destination = isset ($ _POST ['extpath ' ]) ? strip_tags ($ _POST ['extpath ' ]) : '' ;
23
23
$ unzipper ->prepareExtraction ($ archive , $ destination );
24
24
}
25
25
26
26
if (isset ($ _POST ['dozip ' ])) {
27
27
$ zippath = !empty ($ _POST ['zippath ' ]) ? strip_tags ($ _POST ['zippath ' ]) : '. ' ;
28
- // Resulting zipfile e.g. zipper--2016-07-23--11-55.zip
28
+ // Resulting zipfile e.g. zipper--2016-07-23--11-55.zip.
29
29
$ zipfile = 'zipper- ' . date ("Y-m-d--H-i " ) . '.zip ' ;
30
30
Zipper::zipDir ($ zippath , $ zipfile );
31
31
}
32
32
33
33
$ timeend = microtime (TRUE );
34
- $ time = $ timeend - $ timestart ;
34
+ $ time = round ( $ timeend - $ timestart, 4 ) ;
35
35
36
36
/**
37
37
* Class Unzipper
@@ -41,8 +41,7 @@ class Unzipper {
41
41
public $ zipfiles = array ();
42
42
43
43
public function __construct () {
44
-
45
- //read directory and pick .zip and .gz files
44
+ // Read directory and pick .zip, .rar and .gz files.
46
45
if ($ dh = opendir ($ this ->localdir )) {
47
46
while (($ file = readdir ($ dh )) !== FALSE ) {
48
47
if (pathinfo ($ file , PATHINFO_EXTENSION ) === 'zip '
@@ -66,22 +65,24 @@ public function __construct() {
66
65
/**
67
66
* Prepare and check zipfile for extraction.
68
67
*
69
- * @param $archive
70
- * @param $destination
68
+ * @param string $archive
69
+ * The archive name including file extension. E.g. my_archive.zip.
70
+ * @param string $destination
71
+ * The relative destination path where to extract files.
71
72
*/
72
- public function prepareExtraction ($ archive , $ destination ) {
73
+ public function prepareExtraction ($ archive , $ destination = '' ) {
73
74
// Determine paths.
74
75
if (empty ($ destination )) {
75
76
$ extpath = $ this ->localdir ;
76
77
}
77
78
else {
78
79
$ extpath = $ this ->localdir . '/ ' . $ destination ;
79
- // todo move this to extraction function
80
+ // Todo: move this to extraction function.
80
81
if (!is_dir ($ extpath )) {
81
82
mkdir ($ extpath );
82
83
}
83
84
}
84
- //allow only local existing archives to extract
85
+ // Only local existing archives are allowed to be extracted.
85
86
if (in_array ($ archive , $ this ->zipfiles )) {
86
87
self ::extract ($ archive , $ extpath );
87
88
}
@@ -90,8 +91,10 @@ public function prepareExtraction($archive, $destination) {
90
91
/**
91
92
* Checks file extension and calls suitable extractor functions.
92
93
*
93
- * @param $archive
94
- * @param $destination
94
+ * @param string $archive
95
+ * The archive name including file extension. E.g. my_archive.zip.
96
+ * @param string $destination
97
+ * The relative destination path where to extract files.
95
98
*/
96
99
public static function extract ($ archive , $ destination ) {
97
100
$ ext = pathinfo ($ archive , PATHINFO_EXTENSION );
@@ -144,8 +147,10 @@ public static function extractZipArchive($archive, $destination) {
144
147
/**
145
148
* Decompress a .gz File.
146
149
*
147
- * @param $archive
148
- * @param $destination
150
+ * @param string $archive
151
+ * The archive name including file extension. E.g. my_archive.zip.
152
+ * @param string $destination
153
+ * The relative destination path where to extract files.
149
154
*/
150
155
public static function extractGzipFile ($ archive , $ destination ) {
151
156
// Check if zlib is enabled
@@ -177,8 +182,10 @@ public static function extractGzipFile($archive, $destination) {
177
182
/**
178
183
* Decompress/extract a Rar archive using RarArchive.
179
184
*
180
- * @param $archive
181
- * @param $destination
185
+ * @param string $archive
186
+ * The archive name including file extension. E.g. my_archive.zip.
187
+ * @param string $destination
188
+ * The relative destination path where to extract files.
182
189
*/
183
190
public static function extractRarArchive ($ archive , $ destination ) {
184
191
// Check if webserver supports unzipping.
@@ -218,13 +225,13 @@ class Zipper {
218
225
/**
219
226
* Add files and sub-directories in a folder to zip file.
220
227
*
221
- * @param string $folder
228
+ * @param string $folder
222
229
* Path to folder that should be zipped.
223
230
*
224
231
* @param ZipArchive $zipFile
225
232
* Zipfile where files end up.
226
233
*
227
- * @param int $exclusiveLength
234
+ * @param int $exclusiveLength
228
235
* Number of text to be exclusived from the file path.
229
236
*/
230
237
private static function folderToZip ($ folder , &$ zipFile , $ exclusiveLength ) {
@@ -252,6 +259,7 @@ private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
252
259
253
260
/**
254
261
* Zip a folder (including itself).
262
+ *
255
263
* Usage:
256
264
* Zipper::zipDir('path/to/sourceDir', 'path/to/out.zip');
257
265
*
0 commit comments