File tree Expand file tree Collapse file tree 2 files changed +42
-7
lines changed Expand file tree Collapse file tree 2 files changed +42
-7
lines changed Original file line number Diff line number Diff line change @@ -87,24 +87,43 @@ public function open($file)
8787 * @returns FileInfo[]
8888 */
8989 public function contents ()
90+ {
91+ $ result = array ();
92+
93+ foreach ($ this ->yieldContents () as $ fileinfo ) {
94+ $ result [] = $ fileinfo ;
95+ }
96+
97+ return $ result ;
98+ }
99+
100+ /**
101+ * Read the contents of a TAR archive and return each entry using yield
102+ * for memory efficiency.
103+ *
104+ * @see contents()
105+ * @throws ArchiveIOException
106+ * @throws ArchiveCorruptedException
107+ * @returns FileInfo[]
108+ */
109+ public function yieldContents ()
90110 {
91111 if ($ this ->closed || !$ this ->file ) {
92112 throw new ArchiveIOException ('Can not read from a closed archive ' );
93113 }
94114
95- $ result = array ();
96115 while ($ read = $ this ->readbytes (512 )) {
97116 $ header = $ this ->parseHeader ($ read );
98117 if (!is_array ($ header )) {
99118 continue ;
100119 }
101120
102121 $ this ->skipbytes (ceil ($ header ['size ' ] / 512 ) * 512 );
103- $ result [] = $ this ->header2fileinfo ($ header );
122+ yield $ this ->header2fileinfo ($ header );
104123 }
105124
106125 $ this ->close ();
107- return $ result ;
126+
108127 }
109128
110129 /**
Original file line number Diff line number Diff line change @@ -73,24 +73,40 @@ public function open($file)
7373 * @return FileInfo[]
7474 */
7575 public function contents ()
76+ {
77+ $ result = array ();
78+
79+ foreach ($ this ->yieldContents () as $ fileinfo ) {
80+ $ result [] = $ fileinfo ;
81+ }
82+
83+ return $ result ;
84+ }
85+
86+ /**
87+ * Read the contents of a ZIP archive and return each entry using yield
88+ * for memory efficiency.
89+ *
90+ * @see contents()
91+ * @throws ArchiveIOException
92+ * @return FileInfo[]
93+ */
94+ public function yieldContents ()
7695 {
7796 if ($ this ->closed || !$ this ->file ) {
7897 throw new ArchiveIOException ('Can not read from a closed archive ' );
7998 }
8099
81- $ result = array ();
82-
83100 $ centd = $ this ->readCentralDir ();
84101
85102 @rewind ($ this ->fh );
86103 @fseek ($ this ->fh , $ centd ['offset ' ]);
87104
88105 for ($ i = 0 ; $ i < $ centd ['entries ' ]; $ i ++) {
89- $ result [] = $ this ->header2fileinfo ($ this ->readCentralFileHeader ());
106+ yield $ this ->header2fileinfo ($ this ->readCentralFileHeader ());
90107 }
91108
92109 $ this ->close ();
93- return $ result ;
94110 }
95111
96112 /**
You can’t perform that action at this time.
0 commit comments