@@ -9,7 +9,8 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
9
9
filename = "" ,
10
10
fs = Utils . FileSystem . require ( ) ,
11
11
inBuffer = null ,
12
- mainHeader = new Headers . MainHeader ( ) ;
12
+ mainHeader = new Headers . MainHeader ( ) ,
13
+ loadedEntries = false ;
13
14
14
15
if ( inputType === Utils . Constants . FILE ) {
15
16
// is a filename
@@ -22,9 +23,28 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
22
23
readMainHeader ( ) ;
23
24
} else {
24
25
// none. is a new file
26
+ loadedEntries = true ;
27
+ }
28
+
29
+ function iterateEntries ( callback ) {
30
+ const totalEntries = mainHeader . diskEntries ; // total number of entries
31
+ let index = mainHeader . offset ; // offset of first CEN header
32
+
33
+ for ( let i = 0 ; i < totalEntries ; i ++ ) {
34
+ let tmp = index ;
35
+ const entry = new ZipEntry ( inBuffer ) ;
36
+
37
+ entry . header = inBuffer . slice ( tmp , tmp += Utils . Constants . CENHDR ) ;
38
+ entry . entryName = inBuffer . slice ( tmp , tmp += entry . header . fileNameLength ) ;
39
+
40
+ index += entry . header . entryHeaderSize ;
41
+
42
+ callback ( entry ) ;
43
+ }
25
44
}
26
45
27
46
function readEntries ( ) {
47
+ loadedEntries = true ;
28
48
entryTable = { } ;
29
49
entryList = new Array ( mainHeader . diskEntries ) ; // total number of entries
30
50
var index = mainHeader . offset ; // offset of first CEN header
@@ -90,7 +110,7 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
90
110
if ( mainHeader . commentLength ) {
91
111
_comment = inBuffer . slice ( commentEnd + Utils . Constants . ENDHDR ) ;
92
112
}
93
- readEntries ( ) ;
113
+ // readEntries();
94
114
}
95
115
96
116
return {
@@ -99,6 +119,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
99
119
* @return Array
100
120
*/
101
121
get entries ( ) {
122
+ if ( ! loadedEntries ) {
123
+ readEntries ( ) ;
124
+ }
102
125
return entryList ;
103
126
} ,
104
127
@@ -114,13 +137,33 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
114
137
_comment = val ;
115
138
} ,
116
139
140
+ getEntryCount : function ( ) {
141
+ if ( ! loadedEntries ) {
142
+ return mainHeader . diskEntries ;
143
+ }
144
+
145
+ return entryList . length ;
146
+ } ,
147
+
148
+ forEach : function ( callback ) {
149
+ if ( ! loadedEntries ) {
150
+ iterateEntries ( callback ) ;
151
+ return ;
152
+ }
153
+
154
+ entryList . forEach ( callback ) ;
155
+ } ,
156
+
117
157
/**
118
158
* Returns a reference to the entry with the given name or null if entry is inexistent
119
159
*
120
160
* @param entryName
121
161
* @return ZipEntry
122
162
*/
123
163
getEntry : function ( /*String*/ entryName ) {
164
+ if ( ! loadedEntries ) {
165
+ readEntries ( ) ;
166
+ }
124
167
return entryTable [ entryName ] || null ;
125
168
} ,
126
169
@@ -130,6 +173,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
130
173
* @param entry
131
174
*/
132
175
setEntry : function ( /*ZipEntry*/ entry ) {
176
+ if ( ! loadedEntries ) {
177
+ readEntries ( ) ;
178
+ }
133
179
entryList . push ( entry ) ;
134
180
entryTable [ entry . entryName ] = entry ;
135
181
mainHeader . totalEntries = entryList . length ;
@@ -142,6 +188,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
142
188
* @param entryName
143
189
*/
144
190
deleteEntry : function ( /*String*/ entryName ) {
191
+ if ( ! loadedEntries ) {
192
+ readEntries ( ) ;
193
+ }
145
194
var entry = entryTable [ entryName ] ;
146
195
if ( entry && entry . isDirectory ) {
147
196
var _self = this ;
@@ -163,6 +212,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
163
212
* @return Array
164
213
*/
165
214
getEntryChildren : function ( /*ZipEntry*/ entry ) {
215
+ if ( ! loadedEntries ) {
216
+ readEntries ( ) ;
217
+ }
166
218
if ( entry . isDirectory ) {
167
219
var list = [ ] ,
168
220
name = entry . entryName ,
@@ -184,6 +236,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
184
236
* @return Buffer
185
237
*/
186
238
compressToBuffer : function ( ) {
239
+ if ( ! loadedEntries ) {
240
+ readEntries ( ) ;
241
+ }
187
242
if ( entryList . length > 1 ) {
188
243
entryList . sort ( function ( a , b ) {
189
244
var nameA = a . entryName . toLowerCase ( ) ;
@@ -258,6 +313,9 @@ module.exports = function (/*String|Buffer*/input, /*Number*/inputType) {
258
313
} ,
259
314
260
315
toAsyncBuffer : function ( /*Function*/ onSuccess , /*Function*/ onFail , /*Function*/ onItemStart , /*Function*/ onItemEnd ) {
316
+ if ( ! loadedEntries ) {
317
+ readEntries ( ) ;
318
+ }
261
319
if ( entryList . length > 1 ) {
262
320
entryList . sort ( function ( a , b ) {
263
321
var nameA = a . entryName . toLowerCase ( ) ;
0 commit comments