From d3d02f4253e38c42b62dd25cb02a4f3ab35557d9 Mon Sep 17 00:00:00 2001 From: Marco Dickert Date: Tue, 4 Jul 2017 12:56:41 +0200 Subject: [PATCH] consolidate zip class, remove unzip function from main class --- ifm.php | 34 +++++++++++++++------------------- src/ifmzip.php | 17 +++++++++++++---- src/main.php | 17 ++--------------- 3 files changed, 30 insertions(+), 38 deletions(-) diff --git a/ifm.php b/ifm.php index 4c9ccbe..65d8c0a 100644 --- a/ifm.php +++ b/ifm.php @@ -108,6 +108,9 @@ class IFMConfig { */ class IFMZip { + /** + * Add a folder to the zip file + */ private static function folderToZip($folder, &$zipFile, $exclusiveLength) { $handle = opendir( $folder ); while( false !== $f = readdir( $handle ) ) { @@ -129,7 +132,10 @@ class IFMZip { closedir( $handle ); } - public static function create_zip( $src, $out, $root=false ) + /** + * Create a zip file + */ + public static function create( $src, $out, $root=false ) { $z = new ZipArchive(); $z->open( $out, ZIPARCHIVE::CREATE); @@ -148,11 +154,14 @@ class IFMZip { } } - public static function unzip_file( $file ) { - $zip = new ZipArchive(); + /** + * Unzip a zip file + */ + public function extract( $file, $destination="./" ) { + $zip = new ZipArchive; $res = $zip->open( $file ); if( $res === true ) { - $zip->extractTo( './' ); + $zip->extractTo( $destination ); $zip->close(); return true; } else { @@ -1779,7 +1788,7 @@ ifm.init(); echo json_encode( array( "status" => "ERROR","message" => "Could not create target directory." ) ); exit( 1 ); } - if( ! $this->unzip( $d['filename'], $d['targetdir'] ) ) { + if( ! IFMZip::extract( $d['filename'], $d['targetdir'] ) ) { echo json_encode( array( "status" => "ERROR","message" => "File could not be extracted" ) ); } else { echo json_encode( array( "status" => "OK","message" => "File successfully extracted." ) ); @@ -1871,7 +1880,7 @@ ifm.init(); unset( $zip ); $dfile = $this->pathCombine( IFMConfig::tmp_dir, uniqid( "ifm-tmp-" ) . ".zip" ); // temporary filename try { - IFMZip::create_zip( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) ); + IFMZip::create( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) ); if( $d['filename'] == "." ) { if( getcwd() == $this->getScriptRoot() ) $d['filename'] = "root"; @@ -2219,19 +2228,6 @@ ifm.init(); return ( strtolower( $a['name'] ) < strtolower( $b['name'] ) ) ? -1 : 1; } - // unzip an archive - private function unzip( $file, $destination="./" ) { - $zip = new ZipArchive; - $res = $zip->open( $file ); - if( $res === true ) { - $zip->extractTo( $destination ); - $zip->close(); - return true; - } else { - return false; - } - } - // is cURL extention avaliable? private function checkCurl() { if( !function_exists( "curl_init" ) || diff --git a/src/ifmzip.php b/src/ifmzip.php index 5f93593..cbc501e 100644 --- a/src/ifmzip.php +++ b/src/ifmzip.php @@ -13,6 +13,9 @@ */ class IFMZip { + /** + * Add a folder to the zip file + */ private static function folderToZip($folder, &$zipFile, $exclusiveLength) { $handle = opendir( $folder ); while( false !== $f = readdir( $handle ) ) { @@ -34,7 +37,10 @@ class IFMZip { closedir( $handle ); } - public static function create_zip( $src, $out, $root=false ) + /** + * Create a zip file + */ + public static function create( $src, $out, $root=false ) { $z = new ZipArchive(); $z->open( $out, ZIPARCHIVE::CREATE); @@ -53,11 +59,14 @@ class IFMZip { } } - public static function unzip_file( $file ) { - $zip = new ZipArchive(); + /** + * Unzip a zip file + */ + public function extract( $file, $destination="./" ) { + $zip = new ZipArchive; $res = $zip->open( $file ); if( $res === true ) { - $zip->extractTo( './' ); + $zip->extractTo( $destination ); $zip->close(); return true; } else { diff --git a/src/main.php b/src/main.php index 73efef6..14b4761 100644 --- a/src/main.php +++ b/src/main.php @@ -482,7 +482,7 @@ class IFM { echo json_encode( array( "status" => "ERROR","message" => "Could not create target directory." ) ); exit( 1 ); } - if( ! $this->unzip( $d['filename'], $d['targetdir'] ) ) { + if( ! IFMZip::extract( $d['filename'], $d['targetdir'] ) ) { echo json_encode( array( "status" => "ERROR","message" => "File could not be extracted" ) ); } else { echo json_encode( array( "status" => "OK","message" => "File successfully extracted." ) ); @@ -574,7 +574,7 @@ class IFM { unset( $zip ); $dfile = $this->pathCombine( IFMConfig::tmp_dir, uniqid( "ifm-tmp-" ) . ".zip" ); // temporary filename try { - IFMZip::create_zip( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) ); + IFMZip::create( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) ); if( $d['filename'] == "." ) { if( getcwd() == $this->getScriptRoot() ) $d['filename'] = "root"; @@ -922,19 +922,6 @@ class IFM { return ( strtolower( $a['name'] ) < strtolower( $b['name'] ) ) ? -1 : 1; } - // unzip an archive - private function unzip( $file, $destination="./" ) { - $zip = new ZipArchive; - $res = $zip->open( $file ); - if( $res === true ) { - $zip->extractTo( $destination ); - $zip->close(); - return true; - } else { - return false; - } - } - // is cURL extention avaliable? private function checkCurl() { if( !function_exists( "curl_init" ) ||