N°9875 - Error deprecated on component version object - propose boilerplate methods to handle ZipArchive

This commit is contained in:
odain
2026-08-18 17:03:07 +02:00
parent fac132f707
commit ff21eb3bc9
2 changed files with 140 additions and 0 deletions

View File

@@ -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 ];
}
}