Skip to content

Commit f18ee36

Browse files
committed
Simplify adding entries to the zipfile
1 parent bc8b0ee commit f18ee36

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

src/main/php/xp/lambda/PackageLambda.class.php

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php namespace xp\lambda;
22

33
use io\Path;
4-
use io\archive\zip\{ZipFile, ZipDirEntry, ZipFileEntry, Compression};
4+
use io\archive\zip\{ZipFile, ZipArchiveWriter, ZipDirEntry, ZipFileEntry, Compression};
55
use io\streams\StreamTransfer;
66
use util\cmd\Console;
77

@@ -25,26 +25,24 @@ public function __construct(Path $target, Sources $sources, string $exclude= '#(
2525
}
2626
}
2727

28-
/** Returns ZIP file entries */
29-
private function entries(Path $path) {
28+
/** Adds ZIP file entries */
29+
private function add(ZipArchiveWriter $zip, Path $path) {
3030
if (preg_match($this->exclude, $path->toString('/'))) return;
3131

3232
$relative= $path->relativeTo($this->sources->base);
3333
if ($path->isFile()) {
34-
yield function($z) use($relative, $path) {
35-
$file= $z->add(new ZipFileEntry($relative));
34+
$file= $zip->add(new ZipFileEntry($relative));
3635

37-
// See https://stackoverflow.com/questions/46716095/minimum-file-size-for-compression-algorithms
38-
if (filesize($path) > self::COMPRESSION_THRESHOLD) {
39-
$file->setCompression($this->compression, 9);
40-
}
41-
(new StreamTransfer($path->asFile()->in(), $file->out()))->transferAll();
42-
return $file;
43-
};
36+
// See https://stackoverflow.com/questions/46716095/minimum-file-size-for-compression-algorithms
37+
if (filesize($path) > self::COMPRESSION_THRESHOLD) {
38+
$file->setCompression($this->compression, 9);
39+
}
40+
(new StreamTransfer($path->asFile()->in(), $file->out()))->transferAll();
41+
yield $file;
4442
} else {
45-
yield function($z) use($relative) { return $z->add(new ZipDirEntry($relative)); };
43+
yield $zip->add(new ZipDirEntry($relative));
4644
foreach ($path->asFolder()->entries() as $entry) {
47-
yield from $this->entries($entry);
45+
yield from $this->add($zip, $entry);
4846
}
4947
}
5048
}
@@ -58,8 +56,7 @@ public function run(): int {
5856
foreach ($sources as $i => $source) {
5957
Console::writef("\e[34m => [%d/%d] ", $i + 1, $total);
6058
$entries= 0;
61-
foreach ($this->entries(new Path($source)) as $action) {
62-
$entry= $action($z);
59+
foreach ($this->add($z, new Path($source)) as $entry) {
6360
$entries++;
6461
Console::writef('%-60s %4d%s', substr($entry->getName(), -60), $entries, str_repeat("\x08", 65));
6562
}

0 commit comments

Comments
 (0)