diff --git a/lib/filestorage/stored_file.php b/lib/filestorage/stored_file.php index fc01b1ecae7..d04b236b9b0 100644 --- a/lib/filestorage/stored_file.php +++ b/lib/filestorage/stored_file.php @@ -496,6 +496,27 @@ class stored_file { return $this->filesystem->list_files($this, $packer); } + /** + * Returns the total size (in bytes) of the contents of an archive. + * + * @param file_packer $packer file packer instance + * @return int|null total size in bytes + */ + public function get_total_content_size(file_packer $packer): ?int { + // Fetch the contents of the archive. + $files = $this->list_files($packer); + + // Early return if the value of $files is not of type array. + // This can happen when the utility class is unable to open or read the contents of the archive. + if (!is_array($files)) { + return null; + } + + return array_reduce($files, function ($contentsize, $file) { + return $contentsize + $file->size; + }, 0); + } + /** * Extract file to given file path (real OS filesystem), existing files are overwritten. *