19 declare(strict_types=1);
35 private array
$ignored = [
'.',
'..',
'__MACOSX',
'.info',
'.DS_Store'];
39 private \ILIAS\ResourceStorage\Services
$irss;
46 ?
string $current_level = self::BASE ?? self::BASE
50 $this->irss = $DIC->resourceStorage();
51 $this->file_delivery = $DIC->fileDelivery();
53 $this->irss->consume()->stream($rid)->getStream()
67 if ($this->use_flavour) {
68 $flavour_definition = new \ZipStructureDefinition();
69 $flavour = $this->irss->flavours()->get(
73 $flavour_stream = $flavour->getStreamResolvers()[0] ?? null;
74 if ($flavour_stream !== null) {
75 $this->data = $flavour_definition->wake((
string) $flavour_stream->getStream());
79 if (empty($this->data)) {
80 $this->data = $this->reader->getStructure();
84 $this->data = array_filter(
86 static fn($key) => !in_array($key, [
'.',
'/',
'./',
'..'],
true),
91 public function download(
string $path_inside_zip): never
93 [$stream, $info] = $this->reader->getItem($path_inside_zip, $this->data);
95 $supported_for_inline = [
101 static fn(
string $mime_type) => str_replace(
'*',
'.*', preg_quote($mime_type,
'/')),
102 $supported_for_inline
104 $regex = implode(
'|', $regex);
106 $disposition = preg_match(
"/($regex)/", $info[
'mime_type']) ? Disposition::INLINE : Disposition::ATTACHMENT;
108 $this->file_delivery->delivery()->deliver(
116 public function unzip(
string $path_inside_zip):
bool 118 [$stream, $info] = $this->reader->getItem($path_inside_zip, $this->data);
119 if ($info[
'mime_type'] !==
'application/zip' && $info[
'mime_type'] !==
'application/x-zip-compressed') {
124 $tmp_directory = defined(
'CLIENT_DATA_DIR') ?
\CLIENT_DATA_DIR .
'/temp' : sys_get_temp_dir();
125 $tmp_file = tempnam($tmp_directory,
'ilias_zip_');
128 $return = file_put_contents($tmp_file, $stream->detach());
134 foreach ($zip_reader->getStructure() as $append_path_inside_zip => $item) {
135 if ($item[
'is_dir']) {
138 [$stream, $info] = $zip_reader->getItem($append_path_inside_zip, $this->data);
139 $this->irss->manageContainer()->addStreamToContainer(
142 $this->current_level .
'/' . ltrim($append_path_inside_zip,
'./')
153 uasort($this->data,
static function (array
$a, array
$b):
int {
154 if ($a[
'is_dir'] === $b[
'is_dir']) {
155 return strnatcasecmp($a[
'basename'], $b[
'basename']);
157 return $a[
'is_dir'] ? 1 : -1;
160 foreach ($this->data as
$path => $path_data) {
161 $dirname = $path_data[
'dirname'] ??
'./';
162 if ($dirname !== $this->current_level) {
165 $basename = $path_data[
'basename'] ??
'';
166 if (in_array($basename, $this->ignored,
true)) {
170 if ($path_data[
'is_dir'] ??
false) {
176 if ($file !== null) {
188 private function basename(
string $full_path): string
197 $current_level =
'/' . ltrim($current_level,
'./');
198 $current_level = rtrim($current_level,
'/');
199 $this->current_level = $current_level ===
'' ? self::BASE :
$current_level;
202 protected function directory(
string $path_inside_zip, array $data): ?
Dir 211 protected function file(
string $path_inside_zip, array $data): ?
File 216 $data[
'mime_type'] ??
'application/octet-stream',
file(string $path_inside_zip, array $data)
ILIAS ResourceStorage Services $irss
initCurrentLevel(string $current_level)
download(string $path_inside_zip)
basename(string $full_path)
static ofResource($resource)
Wraps an already created resource with the stream abstraction.
directory(string $path_inside_zip, array $data)
Class ResourceIdentification.
__construct(private ResourceIdentification $rid, ?string $current_level=self::BASE ?? self::BASE)
initData(ResourceIdentification $rid)
$a
thx to https://mlocati.github.io/php-cs-fixer-configurator for the examples
ILIAS FileDelivery Services $file_delivery