/
zipfile.js
138 lines (105 loc) · 3.01 KB
/
zipfile.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
var Stream = require('stream').Stream;
var util = require('util')
var zipfile = require('./_zipfile');
var pool;
var kMinPoolSpace = 128;
var kPoolSize = 40 * 1024;
function allocNewPool() {
pool = new Buffer(kPoolSize);
pool.used = 0;
}
zipfile.ZipFile.prototype.createReadStream = function (file) {
return new ReadStream(this, file);
}
var ReadStream = function(zf, file) {
if (!(this instanceof ReadStream)) return new ReadStream(zip, file);
Stream.call(this);
this.readable = true;
this.paused = false;
this.bufferSize = 64 * 1024;
if (this.encoding) this.setEncoding(this.encoding);
this.zf = zf;
zf.open(file);
this._read();
}
util.inherits(ReadStream, Stream);
ReadStream.prototype.setEncoding = function(encoding) {
var StringDecoder = require('string_decoder').StringDecoder; // lazy load
this._decoder = new StringDecoder(encoding);
};
ReadStream.prototype._read = function() {
var self = this;
if (!self.readable || self.paused || self.reading) return;
self.reading = true;
if (!pool || pool.length - pool.used < kMinPoolSpace) {
// discard the old pool. Can't add to the free list because
// users might have refernces to slices on it.
pool = null;
allocNewPool();
}
// Grab another reference to the pool in the case that while we're in the
// thread pool another read() finishes up the pool, and allocates a new
// one.
var thisPool = pool;
var toRead = Math.min(pool.length - pool.used, this.bufferSize);
var start = pool.used;
if (this.pos !== undefined) {
toRead = Math.min(this.end - this.pos + 1, toRead);
}
function afterRead(err, bytesRead) {
self.reading = false;
if (err) {
self.emit('error', err);
self.readable = false;
return;
}
if (bytesRead === 0) {
self.emit('end');
self.destroy();
return;
}
var b = thisPool.slice(start, start + bytesRead);
// Possible optimizition here?
// Reclaim some bytes if bytesRead < toRead?
// Would need to ensure that pool === thisPool.
// do not emit events if the stream is paused
if (self.paused) {
self.buffer = b;
return;
}
// do not emit events anymore after we declared the stream unreadable
if (!self.readable) return;
self._emitData(b);
self._read();
}
self.zf.read(pool, pool.used, toRead, afterRead);
if (self.pos !== undefined) {
self.pos += toRead;
}
pool.used += toRead;
};
ReadStream.prototype._emitData = function(d) {
if (this._decoder) {
var string = this._decoder.write(d);
if (string.length) this.emit('data', string);
} else {
this.emit('data', d);
}
};
ReadStream.prototype.destroy = function() {
this.readable = false;
this.zf.close();
this.emit('close');
};
ReadStream.prototype.pause = function() {
this.paused = true;
};
ReadStream.prototype.resume = function() {
this.paused = false;
if (this.buffer) {
this._emitData(this.buffer);
this.buffer = null;
}
this._read();
};
exports.ZipFile = zipfile.ZipFile;