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

refactor ifmarchive

This commit is contained in:
Dmitriy Novash
2021-11-30 17:52:23 +02:00
parent 8b4635cbdc
commit 8248820c27
2 changed files with 45 additions and 33 deletions

View File

@@ -50,22 +50,22 @@ class IFMArchive {
/** /**
* Create a zip file * Create a zip file
*/ */
public static function createZip($src, $out, $exclude_callback=null) { public static function createZip($filenames, $archivename, $exclude_callback=null) {
$a = new ZipArchive(); $a = new ZipArchive();
$a->open($out, ZIPARCHIVE::CREATE); $a->open($archivename, ZIPARCHIVE::CREATE);
if (!is_array($src)) if (!is_array($filenames))
$src = array($src); $filenames = array($filenames);
foreach ($src as $s) foreach ($filenames as $f)
if (is_dir($s)) if (is_dir($f))
if (is_callable($exclude_callback)) if (is_callable($exclude_callback))
self::addFolder( $a, $s, null, $exclude_callback ); self::addFolder( $a, $f, null, $exclude_callback );
else else
self::addFolder( $a, $s ); self::addFolder( $a, $f );
elseif (is_file($s)) elseif (is_file($f))
if (!is_callable($exclude_callback) || $exclude_callback($s)) if (!is_callable($exclude_callback) || $exclude_callback($f))
$a->addFile($s, substr($s, strlen(dirname($s)) + 1)); $a->addFile($f, pathinfo($f, PATHINFO_BASENAME));
try { try {
return $a->close(); return $a->close();
@@ -93,28 +93,28 @@ class IFMArchive {
/** /**
* Creates a tar archive * Creates a tar archive
*/ */
public static function createTar($src, $out, $t) { public static function createTar($filenames, $archivename, $format) {
$tmpf = substr($out, 0, strlen($out) - strlen($t)) . "tar"; $tmpf = $archivename . ".tar";
$a = new PharData($tmpf); $a = new PharData($tmpf);
try { try {
if (!is_array($src)) if (!is_array($filenames))
$src = array($src); $filenames = array($filenames);
foreach ($src as $s) foreach ($filenames as $f)
if (is_dir($s)) if (is_dir($f))
self::addFolder($a, $s); self::addFolder($a, $f);
elseif (is_file($s)) elseif (is_file($f))
$a->addFile($s, substr($s, strlen(dirname($s)) +1)); $a->addFile($f, pathinfo($f, PATHINFO_BASENAME));
switch ($t) { switch ($format) {
case "tar.gz": case "tar.gz":
$a->compress(Phar::GZ); $a->compress(Phar::GZ);
@unlink($tmpf); @unlink($tmpf);
break; break;
case "tar.bz2": case "tar.bz2":
$a->compress(Phar::BZ2); $a->compress(Phar::BZ2);
@unlink($tmpf); @unlink($tmpf);
break; break;
} }
return true; return true;
} catch (Exception $e) { } catch (Exception $e) {

View File

@@ -686,17 +686,22 @@ f00bar;
$restoreIFM = true; $restoreIFM = true;
} }
if (substr(strtolower($d['filename']), -4) == ".zip") { if (strtolower(pathinfo($d['filename'], PATHINFO_EXTENSION) == "zip")) {
if (!IFMArchive::extractZip($d['filename'], $d['targetdir'])) if (!IFMArchive::extractZip($d['filename'], $d['targetdir']))
throw new IFMException($this->l('extract_error')); throw new IFMException($this->l('extract_error'));
else else
return ["status" => "OK","message" => $this->l('extract_success')]; return ["status" => "OK","message" => $this->l('extract_success')];
} else { } elseif (
(strtolower(pathinfo($d['filename'], PATHINFO_EXTENSION)) == "tar")
|| (strtolower(pathinfo(pathinfo($d['filename'], PATHINFO_FILENAME), PATHINFO_EXTENSION)) == "tar")
) {
if (!IFMArchive::extractTar($d['filename'], $d['targetdir'])) if (!IFMArchive::extractTar($d['filename'], $d['targetdir']))
throw new IFMException($this->l('extract_error')); throw new IFMException($this->l('extract_error'));
else else
return ["status" => "OK","message" => $this->l('extract_success')]; return ["status" => "OK","message" => $this->l('extract_success')];
} } else {
throw new IFMException($this->l('archive_invalid_format'));
}
if ($restoreIFM) { if ($restoreIFM) {
if ($tmpSelfChecksum != hash_file("sha256", __FILE__)) { if ($tmpSelfChecksum != hash_file("sha256", __FILE__)) {
@@ -891,8 +896,15 @@ f00bar;
throw new IFMException($this->l('archive_create_error')); throw new IFMException($this->l('archive_create_error'));
break; break;
case "tar": case "tar":
$d['archivename'] = pathinfo($d['archivename'], PATHINFO_FILENAME);
if (IFMArchive::createTar($filenames, $d['archivename'], $d['format']))
return ["status" => "OK", "message" => $this->l('archive_create_success')];
else
throw new IFMException($this->l('archive_create_error'));
break;
case "tar.gz": case "tar.gz":
case "tar.bz2": case "tar.bz2":
$d['archivename'] = pathinfo(pathinfo($d['archivename'], PATHINFO_FILENAME), PATHINFO_FILENAME);
if (IFMArchive::createTar($filenames, $d['archivename'], $d['format'])) if (IFMArchive::createTar($filenames, $d['archivename'], $d['format']))
return ["status" => "OK", "message" => $this->l('archive_create_success')]; return ["status" => "OK", "message" => $this->l('archive_create_success')];
else else