From ff21eb3bc9c477992da8191b860060356caaea82 Mon Sep 17 00:00:00 2001 From: odain Date: Tue, 18 Aug 2026 17:03:07 +0200 Subject: [PATCH] =?UTF-8?q?=20N=C2=B09875=20-=20Error=20deprecated=20on=20?= =?UTF-8?q?component=20version=20object=20-=20propose=20boilerplate=20meth?= =?UTF-8?q?ods=20to=20handle=20ZipArchive?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- application/utils.inc.php | 43 ++++++++ .../unitary-tests/application/utilsTest.php | 97 +++++++++++++++++++ 2 files changed, 140 insertions(+) diff --git a/application/utils.inc.php b/application/utils.inc.php index 4f0ded9679..72f9943841 100644 --- a/application/utils.inc.php +++ b/application/utils.inc.php @@ -29,6 +29,7 @@ use ScssPhp\ScssPhp\Compiler; use ScssPhp\ScssPhp\OutputStyle; use ScssPhp\ScssPhp\ValueConverter; use Soundasleep\Html2Text; +use ZipArchive; /** * Static class utils @@ -3249,4 +3250,46 @@ TXT return (int)$sLimit; } } + + /** + * Open archive and raise appropriate exception. + * Warning: do not forget to close archive afterwhile + * @param string $sArchiveFilePath + * @param int|null $flags + * @return \ZipArchive + * @throws \Exception + */ + public static function ZipArchiveOpen(string $sArchiveFilePath, int|null $flags = null): \ZipArchive + { + $oZip = new \ZipArchive(); + if (is_null($flags)) { + $code = $oZip->open($sArchiveFilePath); + } else { + $code = $oZip->open($sArchiveFilePath, $flags); + } + if (true !== $code) { + //ZipArchive::ZIP_ER_NOZIP : 19 + if ($code === 19) { + throw new \Exception(sprintf('Cannot to open zip file due to inconsistent or empty content')); + } + + throw new \Exception(sprintf('Cannot to open zip file due to error code %s', $code)); + } + return $oZip; + } + + /** + * @param string $sDirectory + * @param string $sPrefix + * @return array + * @throws \Exception + */ + public static function ZipArchiveOpenWithTempNam(string $sDirectory, string $sPrefix): array + { + $sTempnam = tempnam($sDirectory, $sPrefix); + unlink($sTempnam); + $sArchiveName = $sTempnam.'.zip';; + + return [ self::ZipArchiveOpen($sArchiveName, \ZipArchive::CREATE), $sArchiveName ]; + } } diff --git a/tests/php-unit-tests/unitary-tests/application/utilsTest.php b/tests/php-unit-tests/unitary-tests/application/utilsTest.php index 6084e3453b..51e99f5f85 100644 --- a/tests/php-unit-tests/unitary-tests/application/utilsTest.php +++ b/tests/php-unit-tests/unitary-tests/application/utilsTest.php @@ -1009,4 +1009,101 @@ INI; utils::Unserialize($sData); } + + public static function ZipArchiveOpen_ValidZipFileProvider() + { + return [ + "RDONLY" => [\ZipArchive::RDONLY], + "null" => [null], + ]; + } + + /** + * @dataProvider ZipArchiveOpen_ValidZipFileProvider + */ + public function testZipArchiveOpen_ValidZipFile($flags) + { + $sArchiveName = tempnam(sys_get_temp_dir(), "testZipArchiveOpen_ValidZipFile_"); + unlink($sArchiveName); + $oZip = new \ZipArchive(); + $oZip->open($sArchiveName, \ZipArchive::CREATE); + $oZip->addFile(__FILE__); + $oZip->close(); + + $this->aFileToClean [] = $sArchiveName; + + $oZip = utils::ZipArchiveOpen($sArchiveName, $flags); + self::assertNotNull($oZip); + $oZip->close(); + } + + public static function ZipArchiveOpen_EmptyExistingFileProvider() + { + return [ + "RDONLY" => [\ZipArchive::RDONLY, 'Cannot to open zip file due to inconsistent or empty content'], + "OVERWRITE" => [\ZipArchive::OVERWRITE], + ]; + } + + /** + * @dataProvider ZipArchiveOpen_EmptyExistingFileProvider + */ + public function testZipArchiveOpen_EmptyExistingFile($flags, $sExpectedMessage = null) + { + $sFolderPath = tempnam(sys_get_temp_dir(), "testZipArchiveOpen_ZipFile_"); + $this->aFileToClean [] = $sFolderPath; + + if (! is_null($sExpectedMessage)) { + $this->expectExceptionMessage($sExpectedMessage); + } + $oZip = utils::ZipArchiveOpen($sFolderPath, $flags); + if (is_null($sExpectedMessage)) { + self::assertNotNull($oZip); + $oZip->close(); + touch($sFolderPath); + } + } + + public static function ZipArchiveOpen_NotyExistingFileProvider() + { + return [ + "CREATE" => [\ZipArchive::CREATE], + "null" => [null, 'Cannot to open zip file due to error code 9'], + ]; + } + + /** + * @dataProvider ZipArchiveOpen_NotyExistingFileProvider + */ + public function testZipArchiveOpen_NotyExistingFile($flags, $sExpectedMessage = null) + { + $sFolderPath = tempnam(sys_get_temp_dir(), "testZipArchiveOpen_ZipFile_"); + @unlink($sFolderPath); + + if (! is_null($sExpectedMessage)) { + $this->expectExceptionMessage($sExpectedMessage); + } + $oZip = utils::ZipArchiveOpen($sFolderPath, $flags); + if (is_null($sExpectedMessage)) { + self::assertNotNull($oZip); + $oZip->close(); + touch($sFolderPath); + } + } + + public function testZipArchiveOpenWithTempNam() + { + list($oZip, $sFilePath) = utils::ZipArchiveOpenWithTempNam(sys_get_temp_dir(), "testZipArchiveOpenWithTempFile_"); + self::assertNotNull($oZip); + + self::assertFalse(is_file($sFilePath), $sFilePath); + + $oZip->addEmptyDir('toto'); + $oZip->addFile(__FILE__); + $oZip->close(); + + self::assertTrue(in_array($sFilePath, glob(sys_get_temp_dir() . '/**'))); + self::assertTrue(is_file($sFilePath), $sFilePath); + unlink($sFilePath); + } }