forked from EvanOxfeld/node-unzip
-
Notifications
You must be signed in to change notification settings - Fork 117
/
Copy pathparse.js
251 lines (217 loc) · 7.08 KB
/
parse.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
var util = require('util');
var zlib = require('zlib');
var Stream = require('stream');
var binary = require('binary');
var Promise = require('bluebird');
var PullStream = require('./PullStream');
var NoopStream = require('./NoopStream');
var BufferStream = require('./BufferStream');
var parseExtraField = require('./parseExtraField');
var Buffer = require('./Buffer');
// Backwards compatibility for node versions < 8
if (!Stream.Writable || !Stream.Writable.prototype.destroy)
Stream = require('readable-stream');
var endDirectorySignature = Buffer.alloc(4);
endDirectorySignature.writeUInt32LE(0x06054b50, 0);
function Parse(opts) {
if (!(this instanceof Parse)) {
return new Parse(opts);
}
var self = this;
self._opts = opts || { verbose: false };
PullStream.call(self, self._opts);
self.on('finish',function() {
self.emit('close');
});
self._readRecord().catch(function(e) {
if (!self.__emittedError || self.__emittedError !== e)
self.emit('error',e);
});
}
util.inherits(Parse, PullStream);
Parse.prototype._readRecord = function () {
var self = this;
return self.pull(4).then(function(data) {
if (data.length === 0)
return;
var signature = data.readUInt32LE(0);
if (signature === 0x04034b50) {
return self._readFile();
}
else if (signature === 0x02014b50) {
self.__ended = true;
return self._readCentralDirectoryFileHeader();
}
else if (signature === 0x06054b50) {
return self._readEndOfCentralDirectoryRecord();
}
else if (self.__ended) {
return self.pull(endDirectorySignature).then(function() {
return self._readEndOfCentralDirectoryRecord();
});
}
else
self.emit('error', new Error('invalid signature: 0x' + signature.toString(16)));
});
};
Parse.prototype._readFile = function () {
var self = this;
return self.pull(26).then(function(data) {
var vars = binary.parse(data)
.word16lu('versionsNeededToExtract')
.word16lu('flags')
.word16lu('compressionMethod')
.word16lu('lastModifiedTime')
.word16lu('lastModifiedDate')
.word32lu('crc32')
.word32lu('compressedSize')
.word32lu('uncompressedSize')
.word16lu('fileNameLength')
.word16lu('extraFieldLength')
.vars;
return self.pull(vars.fileNameLength).then(function(fileNameBuffer) {
fileName = fileNameBuffer.toString('utf8');
var entry = Stream.PassThrough();
var __autodraining = false;
entry.autodrain = function() {
__autodraining = true;
var draining = entry.pipe(NoopStream());
draining.promise = function() {
return new Promise(function(resolve, reject) {
draining.on('finish',resolve);
draining.on('error',reject);
});
};
return draining;
};
entry.buffer = function() {
return BufferStream(entry);
};
entry.path = fileName;
entry.props = {};
entry.props.path = fileName;
entry.props.pathBuffer = fileNameBuffer;
entry.props.flags = {
"isUnicode": vars.flags & 0x11
};
entry.type = (vars.uncompressedSize === 0 && /[\/\\]$/.test(fileName)) ? 'Directory' : 'File';
if (self._opts.verbose) {
if (entry.type === 'Directory') {
console.log(' creating:', fileName);
} else if (entry.type === 'File') {
if (vars.compressionMethod === 0) {
console.log(' extracting:', fileName);
} else {
console.log(' inflating:', fileName);
}
}
}
return self.pull(vars.extraFieldLength).then(function(extraField) {
var extra = parseExtraField(extraField, vars);
entry.vars = vars;
entry.extra = extra;
self.emit('entry', entry);
if (self._readableState.pipesCount)
self.push(entry);
if (self._opts.verbose)
console.log({
filename:fileName,
vars: vars,
extra: extra
});
var fileSizeKnown = !(vars.flags & 0x08),
eof;
entry.__autodraining = __autodraining; // expose __autodraining for test purposes
var inflater = (vars.compressionMethod && !__autodraining) ? zlib.createInflateRaw() : Stream.PassThrough();
if (fileSizeKnown) {
entry.size = vars.uncompressedSize;
eof = vars.compressedSize;
} else {
eof = Buffer.alloc(4);
eof.writeUInt32LE(0x08074b50, 0);
}
self.stream(eof)
.pipe(inflater)
.on('error',function(err) { self.emit('error',err);})
.pipe(entry)
.on('finish', function() {
return fileSizeKnown ? self._readRecord() : self._processDataDescriptor(entry);
});
return null; // This prevents bluebird from throwing "promise created but not returned" warnings
});
});
});
};
Parse.prototype._processDataDescriptor = function (entry) {
var self = this;
self.pull(16).then(function(data) {
var vars = binary.parse(data)
.word32lu('dataDescriptorSignature')
.word32lu('crc32')
.word32lu('compressedSize')
.word32lu('uncompressedSize')
.vars;
entry.size = vars.uncompressedSize;
self._readRecord();
});
};
Parse.prototype._readCentralDirectoryFileHeader = function () {
var self = this;
self.pull(42).then(function(data) {
var vars = binary.parse(data)
.word16lu('versionMadeBy')
.word16lu('versionsNeededToExtract')
.word16lu('flags')
.word16lu('compressionMethod')
.word16lu('lastModifiedTime')
.word16lu('lastModifiedDate')
.word32lu('crc32')
.word32lu('compressedSize')
.word32lu('uncompressedSize')
.word16lu('fileNameLength')
.word16lu('extraFieldLength')
.word16lu('fileCommentLength')
.word16lu('diskNumber')
.word16lu('internalFileAttributes')
.word32lu('externalFileAttributes')
.word32lu('offsetToLocalFileHeader')
.vars;
return self.pull(vars.fileNameLength).then(function(fileName) {
vars.fileName = fileName.toString('utf8');
return self.pull(vars.extraFieldLength);
})
.then(function(extraField) {
return self.pull(vars.fileCommentLength);
})
.then(function(fileComment) {
return self._readRecord();
});
});
};
Parse.prototype._readEndOfCentralDirectoryRecord = function() {
var self = this;
self.pull(18).then(function(data) {
var vars = binary.parse(data)
.word16lu('diskNumber')
.word16lu('diskStart')
.word16lu('numberOfRecordsOnDisk')
.word16lu('numberOfRecords')
.word32lu('sizeOfCentralDirectory')
.word32lu('offsetToStartOfCentralDirectory')
.word16lu('commentLength')
.vars;
self.pull(vars.commentLength).then(function(comment) {
comment = comment.toString('utf8');
self.end();
self.push(null);
});
});
};
Parse.prototype.promise = function() {
var self = this;
return new Promise(function(resolve,reject) {
self.on('finish',resolve);
self.on('error',reject);
});
};
module.exports = Parse;