From f8aeb575828bcbc50e82d3ffbbe62f147e2732a2 Mon Sep 17 00:00:00 2001 From: fbsanchez Date: Tue, 29 Jun 2021 20:03:00 +0200 Subject: [PATCH] add method to test existing and at first look valid zip file --- pandora_console/include/lib/Tools/Files.php | 30 +++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/pandora_console/include/lib/Tools/Files.php b/pandora_console/include/lib/Tools/Files.php index 345d5e4115..903b167878 100644 --- a/pandora_console/include/lib/Tools/Files.php +++ b/pandora_console/include/lib/Tools/Files.php @@ -44,11 +44,11 @@ class Files * @param string $filename Path to target zip file. * @param string $path Directory to be zipped. * - * @return void + * @return integer The number of files added to the zip file. */ public static function zip(string $filename, string $path) { - // Generate a collections zip for Metaconsole. + $added_files = 0; $zip = new \ZipArchive(); $zip->open( $filename, @@ -82,6 +82,7 @@ class Files // Add current file to archive. $zip->addFile($filePath, $relativePath); + $added_files++; // Keep file permissions. $zip->setExternalAttributesName( @@ -93,6 +94,31 @@ class Files } $zip->close(); + + return $added_files; + } + + + /** + * Test if given file is zip file. + * + * @param string $file File. + * + * @return boolean + */ + public static function testZip(string $file):bool + { + if (file_exists($file) === false) { + return false; + } + + $zip = new \ZipArchive; + if ($zip->open($file, (\ZipArchive::RDONLY & \ZipArchive::ER_READ)) === true) { + $zip->close(); + return true; + } + + return false; }