From cef78124722dd301ea4e35df1edf1e3aaf5ab0a4 Mon Sep 17 00:00:00 2001 From: Andy Miller Date: Tue, 2 Sep 2025 15:04:51 -0600 Subject: [PATCH] fix an error in ZipArchive that sometimes occurs Signed-off-by: Andy Miller --- .../Grav/Common/Filesystem/ZipArchiver.php | 41 ++++++++++++++++--- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/system/src/Grav/Common/Filesystem/ZipArchiver.php b/system/src/Grav/Common/Filesystem/ZipArchiver.php index 26c393772..770e84ab8 100644 --- a/system/src/Grav/Common/Filesystem/ZipArchiver.php +++ b/system/src/Grav/Common/Filesystem/ZipArchiver.php @@ -64,8 +64,21 @@ class ZipArchiver extends Archiver } $zip = new ZipArchive(); - if (!$zip->open($this->archive_file, ZipArchive::CREATE)) { - throw new InvalidArgumentException('ZipArchiver:' . $this->archive_file . ' cannot be created...'); + $result = $zip->open($this->archive_file, ZipArchive::CREATE); + if ($result !== true) { + $error = 'unknown error'; + if ($result === ZipArchive::ER_NOENT) { + $error = 'file does not exist'; + } elseif ($result === ZipArchive::ER_EXISTS) { + $error = 'file already exists'; + } elseif ($result === ZipArchive::ER_OPEN) { + $error = 'cannot open file'; + } elseif ($result === ZipArchive::ER_READ) { + $error = 'read error'; + } elseif ($result === ZipArchive::ER_SEEK) { + $error = 'seek error'; + } + throw new InvalidArgumentException('ZipArchiver: ' . $this->archive_file . ' cannot be created: ' . $error); } $files = $this->getArchiveFiles($rootPath); @@ -112,8 +125,21 @@ class ZipArchiver extends Archiver } $zip = new ZipArchive(); - if (!$zip->open($this->archive_file)) { - throw new InvalidArgumentException('ZipArchiver: ' . $this->archive_file . ' cannot be opened...'); + $result = $zip->open($this->archive_file); + if ($result !== true) { + $error = 'unknown error'; + if ($result === ZipArchive::ER_NOENT) { + $error = 'file does not exist'; + } elseif ($result === ZipArchive::ER_EXISTS) { + $error = 'file already exists'; + } elseif ($result === ZipArchive::ER_OPEN) { + $error = 'cannot open file'; + } elseif ($result === ZipArchive::ER_READ) { + $error = 'read error'; + } elseif ($result === ZipArchive::ER_SEEK) { + $error = 'seek error'; + } + throw new InvalidArgumentException('ZipArchiver: ' . $this->archive_file . ' cannot be opened: ' . $error); } $status && $status([ @@ -122,7 +148,12 @@ class ZipArchiver extends Archiver ]); foreach ($folders as $folder) { - $zip->addEmptyDir($folder); + if ($zip->addEmptyDir($folder) === false) { + $status && $status([ + 'type' => 'message', + 'message' => 'Warning: Could not add empty directory: ' . $folder + ]); + } $status && $status([ 'type' => 'progress', ]);