1
0
mirror of https://github.com/misterunknown/ifm.git synced 2025-08-11 10:34:00 +02:00

consolidate zip class, remove unzip function from main class

This commit is contained in:
Marco Dickert
2017-07-04 12:56:41 +02:00
parent c97c0f38dd
commit d3d02f4253
3 changed files with 30 additions and 38 deletions

34
ifm.php
View File

@@ -108,6 +108,9 @@ class IFMConfig {
*/ */
class IFMZip { class IFMZip {
/**
* Add a folder to the zip file
*/
private static function folderToZip($folder, &$zipFile, $exclusiveLength) { private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir( $folder ); $handle = opendir( $folder );
while( false !== $f = readdir( $handle ) ) { while( false !== $f = readdir( $handle ) ) {
@@ -129,7 +132,10 @@ class IFMZip {
closedir( $handle ); 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 = new ZipArchive();
$z->open( $out, ZIPARCHIVE::CREATE); $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 ); $res = $zip->open( $file );
if( $res === true ) { if( $res === true ) {
$zip->extractTo( './' ); $zip->extractTo( $destination );
$zip->close(); $zip->close();
return true; return true;
} else { } else {
@@ -1779,7 +1788,7 @@ ifm.init();
echo json_encode( array( "status" => "ERROR","message" => "Could not create target directory." ) ); echo json_encode( array( "status" => "ERROR","message" => "Could not create target directory." ) );
exit( 1 ); 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" ) ); echo json_encode( array( "status" => "ERROR","message" => "File could not be extracted" ) );
} else { } else {
echo json_encode( array( "status" => "OK","message" => "File successfully extracted." ) ); echo json_encode( array( "status" => "OK","message" => "File successfully extracted." ) );
@@ -1871,7 +1880,7 @@ ifm.init();
unset( $zip ); unset( $zip );
$dfile = $this->pathCombine( IFMConfig::tmp_dir, uniqid( "ifm-tmp-" ) . ".zip" ); // temporary filename $dfile = $this->pathCombine( IFMConfig::tmp_dir, uniqid( "ifm-tmp-" ) . ".zip" ); // temporary filename
try { try {
IFMZip::create_zip( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) ); IFMZip::create( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) );
if( $d['filename'] == "." ) { if( $d['filename'] == "." ) {
if( getcwd() == $this->getScriptRoot() ) if( getcwd() == $this->getScriptRoot() )
$d['filename'] = "root"; $d['filename'] = "root";
@@ -2219,19 +2228,6 @@ ifm.init();
return ( strtolower( $a['name'] ) < strtolower( $b['name'] ) ) ? -1 : 1; 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? // is cURL extention avaliable?
private function checkCurl() { private function checkCurl() {
if( !function_exists( "curl_init" ) || if( !function_exists( "curl_init" ) ||

View File

@@ -13,6 +13,9 @@
*/ */
class IFMZip { class IFMZip {
/**
* Add a folder to the zip file
*/
private static function folderToZip($folder, &$zipFile, $exclusiveLength) { private static function folderToZip($folder, &$zipFile, $exclusiveLength) {
$handle = opendir( $folder ); $handle = opendir( $folder );
while( false !== $f = readdir( $handle ) ) { while( false !== $f = readdir( $handle ) ) {
@@ -34,7 +37,10 @@ class IFMZip {
closedir( $handle ); 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 = new ZipArchive();
$z->open( $out, ZIPARCHIVE::CREATE); $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 ); $res = $zip->open( $file );
if( $res === true ) { if( $res === true ) {
$zip->extractTo( './' ); $zip->extractTo( $destination );
$zip->close(); $zip->close();
return true; return true;
} else { } else {

View File

@@ -482,7 +482,7 @@ class IFM {
echo json_encode( array( "status" => "ERROR","message" => "Could not create target directory." ) ); echo json_encode( array( "status" => "ERROR","message" => "Could not create target directory." ) );
exit( 1 ); 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" ) ); echo json_encode( array( "status" => "ERROR","message" => "File could not be extracted" ) );
} else { } else {
echo json_encode( array( "status" => "OK","message" => "File successfully extracted." ) ); echo json_encode( array( "status" => "OK","message" => "File successfully extracted." ) );
@@ -574,7 +574,7 @@ class IFM {
unset( $zip ); unset( $zip );
$dfile = $this->pathCombine( IFMConfig::tmp_dir, uniqid( "ifm-tmp-" ) . ".zip" ); // temporary filename $dfile = $this->pathCombine( IFMConfig::tmp_dir, uniqid( "ifm-tmp-" ) . ".zip" ); // temporary filename
try { try {
IFMZip::create_zip( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) ); IFMZip::create( realpath( $d['filename'] ), $dfile, ( $d['filename'] == "." ) );
if( $d['filename'] == "." ) { if( $d['filename'] == "." ) {
if( getcwd() == $this->getScriptRoot() ) if( getcwd() == $this->getScriptRoot() )
$d['filename'] = "root"; $d['filename'] = "root";
@@ -922,19 +922,6 @@ class IFM {
return ( strtolower( $a['name'] ) < strtolower( $b['name'] ) ) ? -1 : 1; 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? // is cURL extention avaliable?
private function checkCurl() { private function checkCurl() {
if( !function_exists( "curl_init" ) || if( !function_exists( "curl_init" ) ||