11<?php namespace xp \lambda ;
22
33use io \Path ;
4- use io \archive \zip \{ZipFile , ZipDirEntry , ZipFileEntry , Compression };
4+ use io \archive \zip \{ZipFile , ZipArchiveWriter , ZipDirEntry , ZipFileEntry , Compression };
55use io \streams \StreamTransfer ;
66use 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