88 * @author Andreas Tasch, at[tec], attec.at
99 * @license GNU GPL v3
1010 * @package attec.toolbox
11- * @version 0.1.0
11+ * @version 0.1.1
1212 */
13- define ('VERSION ' , '0.1.0 ' );
13+ define ('VERSION ' , '0.1.1 ' );
1414
1515$ timestart = microtime (TRUE );
1616$ GLOBALS ['status ' ] = array ();
1717
1818$ unzipper = new Unzipper ;
1919if (isset ($ _POST ['dounzip ' ])) {
20- //check if an archive was selected for unzipping
20+ // Check if an archive was selected for unzipping.
2121 $ archive = isset ($ _POST ['zipfile ' ]) ? strip_tags ($ _POST ['zipfile ' ]) : '' ;
2222 $ destination = isset ($ _POST ['extpath ' ]) ? strip_tags ($ _POST ['extpath ' ]) : '' ;
2323 $ unzipper ->prepareExtraction ($ archive , $ destination );
2424}
2525
2626if (isset ($ _POST ['dozip ' ])) {
2727 $ 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.
2929 $ zipfile = 'zipper- ' . date ("Y-m-d--H-i " ) . '.zip ' ;
3030 Zipper::zipDir ($ zippath , $ zipfile );
3131}
3232
3333$ timeend = microtime (TRUE );
34- $ time = $ timeend - $ timestart ;
34+ $ time = round ( $ timeend - $ timestart, 4 ) ;
3535
3636/**
3737 * Class Unzipper
@@ -41,8 +41,7 @@ class Unzipper {
4141 public $ zipfiles = array ();
4242
4343 public function __construct () {
44-
45- //read directory and pick .zip and .gz files
44+ // Read directory and pick .zip, .rar and .gz files.
4645 if ($ dh = opendir ($ this ->localdir )) {
4746 while (($ file = readdir ($ dh )) !== FALSE ) {
4847 if (pathinfo ($ file , PATHINFO_EXTENSION ) === 'zip '
@@ -66,22 +65,24 @@ public function __construct() {
6665 /**
6766 * Prepare and check zipfile for extraction.
6867 *
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.
7172 */
72- public function prepareExtraction ($ archive , $ destination ) {
73+ public function prepareExtraction ($ archive , $ destination = '' ) {
7374 // Determine paths.
7475 if (empty ($ destination )) {
7576 $ extpath = $ this ->localdir ;
7677 }
7778 else {
7879 $ extpath = $ this ->localdir . '/ ' . $ destination ;
79- // todo move this to extraction function
80+ // Todo: move this to extraction function.
8081 if (!is_dir ($ extpath )) {
8182 mkdir ($ extpath );
8283 }
8384 }
84- //allow only local existing archives to extract
85+ // Only local existing archives are allowed to be extracted.
8586 if (in_array ($ archive , $ this ->zipfiles )) {
8687 self ::extract ($ archive , $ extpath );
8788 }
@@ -90,8 +91,10 @@ public function prepareExtraction($archive, $destination) {
9091 /**
9192 * Checks file extension and calls suitable extractor functions.
9293 *
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.
9598 */
9699 public static function extract ($ archive , $ destination ) {
97100 $ ext = pathinfo ($ archive , PATHINFO_EXTENSION );
@@ -144,8 +147,10 @@ public static function extractZipArchive($archive, $destination) {
144147 /**
145148 * Decompress a .gz File.
146149 *
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.
149154 */
150155 public static function extractGzipFile ($ archive , $ destination ) {
151156 // Check if zlib is enabled
@@ -177,8 +182,10 @@ public static function extractGzipFile($archive, $destination) {
177182 /**
178183 * Decompress/extract a Rar archive using RarArchive.
179184 *
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.
182189 */
183190 public static function extractRarArchive ($ archive , $ destination ) {
184191 // Check if webserver supports unzipping.
@@ -218,13 +225,13 @@ class Zipper {
218225 /**
219226 * Add files and sub-directories in a folder to zip file.
220227 *
221- * @param string $folder
228+ * @param string $folder
222229 * Path to folder that should be zipped.
223230 *
224231 * @param ZipArchive $zipFile
225232 * Zipfile where files end up.
226233 *
227- * @param int $exclusiveLength
234+ * @param int $exclusiveLength
228235 * Number of text to be exclusived from the file path.
229236 */
230237 private static function folderToZip ($ folder , &$ zipFile , $ exclusiveLength ) {
@@ -252,6 +259,7 @@ private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
252259
253260 /**
254261 * Zip a folder (including itself).
262+ *
255263 * Usage:
256264 * Zipper::zipDir('path/to/sourceDir', 'path/to/out.zip');
257265 *
0 commit comments