forked from sanatgersappa/node-zipstream
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zipstream.js
executable file
·293 lines (235 loc) · 8.45 KB
/
zipstream.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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// written by Antoine van Wel (http://wellawaretech.com)
// edited by @dmmalam to make it a proper node stream
var zlib = require('zlib');
var fs = require('fs');
var assert = require('assert');
var stream = require('stream');
var util = require('util');
var crc32 = require('./crc32');
function ZipStream(opt) {
var self = this;
self.readable = true;
self.paused = false;
self.busy = false;
self.eof = false;
self.queue = [];
self.fileptr = 0;
self.files = [];
self.options = opt;
}
util.inherits(ZipStream, stream.Stream);
exports.createZip = function(opt) {
return new ZipStream(opt);
}
// converts datetime to DOS format
function convertDate(d) {
var year = d.getFullYear();
if (year < 1980) {
return (1<<21) | (1<<16);
}
return ((year-1980) << 25) | ((d.getMonth()+1) << 21) | (d.getDate() << 16) |
(d.getHours() << 11) | (d.getMinutes() << 5) | (d.getSeconds() >> 1);
}
ZipStream.prototype.pause = function() {
// console.log('zipstream::PAUSE');
var self = this;
self.paused = true;
self.deflate.pause();
}
ZipStream.prototype.resume = function() {
// console.log('zipstream::RESUME');
var self = this;
self.paused = false;
self.deflate.resume();
}
ZipStream.prototype.destroy = function() {
// console.log('zipstream::DESTROY');
var self = this;
self.readable = false;
self.source.destroy();
}
ZipStream.prototype.destroySoon = function() {
// console.log('zipstream::DESTROYSOON');
var self = this;
self.readable = false;
if (self.source.destroySoon){
self.source.destroySoon();
} else {
self.source.destroy();
}
}
ZipStream.prototype.end = function(cb) {
// console.log('zipstream::END');
var self = this;
if (self.files.length === 0) {
emit('error', 'no files in zip');
return;
}
self._pushCentralDirectory();
self.eof = true;
self.readable = false;
self.emit('end');
}
ZipStream.prototype.addFile = function(source, file, callback) {
var self = this;
if (self.busy) {
emit('error', new Error('previous file not finished'));
return false;
}
if (!source.readable) {
emit('error', new Error('Source not readable'));
return false;
}
self.busy = true;
self.file = file;
self.source = source;
self._pushLocalFileHeader(file);
var deflate = self.deflate = zlib.createDeflateRaw(self.options);
var checksum = crc32.createCRC32();
var uncompressed = 0;
var compressed = 0;
var deflateOnData = function(chunk) {
// console.log('deflate::DATA');
compressed += chunk.length;
self.emit('data', chunk);
}
deflate.on('data', deflateOnData);
var deflateOnEnd = function() {
// console.log('deflate::END');
file.crc32 = checksum.digest();
file.compressed = compressed;
file.uncompressed = uncompressed;
self.fileptr += compressed;
self._pushDataDescriptor(file);
self.files.push(file);
self.busy = false;
cleanup()
callback();
}
deflate.on('end', deflateOnEnd);
var deflateOnDrain = function(){
// console.log('deflate::DRAIN');
source.resume();
}
deflate.on('drain', deflateOnDrain);
var deflateOnError = function(err){
// console.log('deflate::ERROR');
source.destroy();
self.readable = false;
cleanup()
self.emit('error',err);
}
deflate.on('error', deflateOnError);
var sourceOnData = function(data) {
// console.log('source::DATA');
uncompressed += data.length;
checksum.update(data);
if (deflate.writable && !deflate.write(data) && source.pause) {
source.pause();
}
}
source.on('data', sourceOnData);
var sourceOnEnd = function() {
// console.log('source::END');
deflate.end();
}
source.on('end', sourceOnEnd);
sourceOnError = function(err){
// console.log('source::ERROR');
deflate.end();
self.readable = false;
cleanup()
self.emit('error',err);
}
source.on('error', sourceOnError);
function cleanup(){
deflate.removeListener('data', deflateOnData);
deflate.removeListener('end', deflateOnEnd);
deflate.removeListener('drain', deflateOnDrain);
deflate.removeListener('error', deflateOnError);
source.removeListener('data', sourceOnData);
source.removeListener('end', sourceOnEnd);
source.removeListener('error', sourceOnError);
}
return true;
}
// local file header
ZipStream.prototype._pushLocalFileHeader = function(file) {
var self = this;
file.version = 20;
file.bitflag = 8;
file.method = 8;
file.moddate = convertDate(new Date());
file.offset = self.fileptr;
var buf = new Buffer(30+file.name.length);
buf.writeUInt32LE(0x04034b50, 0); // local file header signature
buf.writeUInt16LE(file.version, 4); // version needed to extract
buf.writeUInt16LE(file.bitflag, 6); // general purpose bit flag
buf.writeUInt16LE(file.method, 8); // compression method
buf.writeUInt32LE(file.moddate, 10); // last mod file date and time
buf.writeInt32LE(0, 14); // crc32
buf.writeUInt32LE(0, 18); // compressed size
buf.writeUInt32LE(0, 22); // uncompressed size
buf.writeUInt16LE(file.name.length, 26); // file name length
buf.writeUInt16LE(0, 28); // extra field length
buf.write(file.name, 30); // file name
self.emit('data', buf);
self.fileptr += buf.length;
}
ZipStream.prototype._pushDataDescriptor = function(file) {
var self = this;
var buf = new Buffer(16);
buf.writeUInt32LE(0x08074b50, 0); // data descriptor record signature
buf.writeInt32LE(file.crc32, 4); // crc-32
buf.writeUInt32LE(file.compressed, 8); // compressed size
buf.writeUInt32LE(file.uncompressed, 12); // uncompressed size
self.emit('data', buf);
self.fileptr += buf.length;
}
ZipStream.prototype._pushCentralDirectory = function() {
var self = this;
var cdoffset = self.fileptr;
var bufferLength = 600*1024;
var buf = new Buffer(bufferLength); // big archives need a big buffer
var ptr = 0;
var cdsize = 0;
for (var i=0; i<self.files.length; i++) {
var file = self.files[i];
if (ptr + 46 + file.name.length >= bufferLength)
{
console.log("_pushCentralDirectory: Buffer of " + bufferLength + " bytes will be exceed by file " + i + " of " + self.files.length);
}
// central directory file header
buf.writeUInt32LE(0x02014b50, ptr+0); // central file header signature
buf.writeUInt16LE(file.version, ptr+4); // TODO version made by
buf.writeUInt16LE(file.version, ptr+6); // version needed to extract
buf.writeUInt16LE(file.bitflag, ptr+8); // general purpose bit flag
buf.writeUInt16LE(file.method, ptr+10); // compression method
buf.writeUInt32LE(file.moddate, ptr+12); // last mod file time and date
buf.writeInt32LE(file.crc32, ptr+16); // crc-32
buf.writeUInt32LE(file.compressed, ptr+20); // compressed size
buf.writeUInt32LE(file.uncompressed, ptr+24); // uncompressed size
buf.writeUInt16LE(file.name.length, ptr+28); // file name length
buf.writeUInt16LE(0, ptr+30); // extra field length
buf.writeUInt16LE(0, ptr+32); // file comment length
buf.writeUInt16LE(0, ptr+34); // disk number where file starts
buf.writeUInt16LE(0, ptr+36); // internal file attributes
buf.writeUInt32LE(0, ptr+38); // external file attributes
buf.writeUInt32LE(file.offset, ptr+42); // relative offset
buf.write(file.name, ptr+46); // file name
ptr = ptr + 46 + file.name.length;
}
cdsize = ptr;
// end of central directory record
buf.writeUInt32LE(0x06054b50, ptr+0); // end of central dir signature
buf.writeUInt16LE(0, ptr+4); // number of this disk
buf.writeUInt16LE(0, ptr+6); // disk where central directory starts
buf.writeUInt16LE(self.files.length, ptr+8); // number of central directory records on this disk
buf.writeUInt16LE(self.files.length, ptr+10); // total number of central directory records
buf.writeUInt32LE(cdsize, ptr+12); // size of central directory in bytes
buf.writeUInt32LE(cdoffset, ptr+16); // offset of start of central directory, relative to start of archive
buf.writeUInt16LE(0, ptr+20); // comment length
ptr = ptr + 22;
self.emit('data', buf.slice(0, ptr));
self.fileptr += ptr;
}