forked from max-mapper/extract-zip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
205 lines (166 loc) · 6 KB
/
index.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
var fs = require('fs')
var path = require('path')
var yauzl = require('yauzl')
var mkdirp = require('mkdirp')
var concat = require('concat-stream')
var debug = require('debug')('extract-zip')
module.exports = function (zipPath, opts, cb) {
debug('creating target directory', opts.dir)
if (path.isAbsolute(opts.dir) === false) {
return cb(new Error('Target directory is expected to be absolute'))
}
mkdirp(opts.dir, function (err) {
if (err) return cb(err)
fs.realpath(opts.dir, function (err, canonicalDir) {
if (err) return cb(err)
opts.dir = canonicalDir
openZip(opts)
})
})
function openZip () {
debug('opening', zipPath, 'with opts', opts)
yauzl.open(zipPath, {lazyEntries: true}, function (err, zipfile) {
if (err) return cb(err)
var cancelled = false
zipfile.readEntry()
zipfile.on('close', function () {
if (!cancelled) {
debug('zip extraction complete')
cb()
}
})
zipfile.on('entry', function (entry) {
if (cancelled) {
debug('skipping entry', entry.fileName, {cancelled: cancelled})
return
}
debug('zipfile entry', entry.fileName)
if (/^__MACOSX\//.test(entry.fileName)) {
// dir name starts with __MACOSX/
zipfile.readEntry()
return
}
var destDir = path.dirname(path.join(opts.dir, entry.fileName))
mkdirp(destDir, function (err) {
if (err) {
cancelled = true
zipfile.close()
return cb(err)
}
fs.realpath(destDir, function (err, canonicalDestDir) {
if (err) {
cancelled = true
zipfile.close()
return cb(err)
}
var relativeDestDir = path.relative(opts.dir, canonicalDestDir)
if (relativeDestDir.split(path.sep).indexOf('..') !== -1) {
cancelled = true
zipfile.close()
return cb(new Error('Out of bound path "' + canonicalDestDir + '" found while processing file ' + entry.fileName))
}
extractEntry(entry, function (err) {
// if any extraction fails then abort everything
if (err) {
cancelled = true
zipfile.close()
return cb(err)
}
debug('finished processing', entry.fileName)
zipfile.readEntry()
})
})
})
})
function extractEntry (entry, done) {
if (cancelled) {
debug('skipping entry extraction', entry.fileName, {cancelled: cancelled})
return setImmediate(done)
}
if (opts.onEntry) {
opts.onEntry(entry, zipfile)
}
var dest = path.join(opts.dir, entry.fileName)
// convert external file attr int into a fs stat mode int
var mode = (entry.externalFileAttributes >> 16) & 0xFFFF
// check if it's a symlink or dir (using stat mode constants)
var IFMT = 61440
var IFDIR = 16384
var IFLNK = 40960
var symlink = (mode & IFMT) === IFLNK
var isDir = (mode & IFMT) === IFDIR
// Failsafe, borrowed from jsZip
if (!isDir && entry.fileName.slice(-1) === '/') {
isDir = true
}
// check for windows weird way of specifying a directory
// https://github.com/maxogden/extract-zip/issues/13#issuecomment-154494566
var madeBy = entry.versionMadeBy >> 8
if (!isDir) isDir = (madeBy === 0 && entry.externalFileAttributes === 16)
// if no mode then default to default modes
if (mode === 0) {
if (isDir) {
if (opts.defaultDirMode) mode = parseInt(opts.defaultDirMode, 10)
if (!mode) mode = 493 // Default to 0755
} else {
if (opts.defaultFileMode) mode = parseInt(opts.defaultFileMode, 10)
if (!mode) mode = 420 // Default to 0644
}
}
debug('extracting entry', { filename: entry.fileName, isDir: isDir, isSymlink: symlink })
// reverse umask first (~)
var umask = ~process.umask()
// & with processes umask to override invalid perms
var procMode = mode & umask
// always ensure folders are created
var destDir = dest
if (!isDir) destDir = path.dirname(dest)
debug('mkdirp', {dir: destDir})
mkdirp(destDir, function (err) {
if (err) {
debug('mkdirp error', destDir, {error: err})
cancelled = true
return done(err)
}
if (isDir) return done()
debug('opening read stream', dest)
zipfile.openReadStream(entry, function (err, readStream) {
if (err) {
debug('openReadStream error', err)
cancelled = true
return done(err)
}
readStream.on('error', function (err) {
console.log('read err', err)
})
if (symlink) writeSymlink()
else writeStream()
function writeStream () {
var writeStream = fs.createWriteStream(dest, {mode: procMode})
readStream.pipe(writeStream)
writeStream.on('finish', function () {
done()
})
writeStream.on('error', function (err) {
debug('write error', {error: err})
cancelled = true
return done(err)
})
}
// AFAICT the content of the symlink file itself is the symlink target filename string
function writeSymlink () {
readStream.pipe(concat(function (data) {
var link = data.toString()
debug('creating symlink', link, dest)
fs.symlink(link, dest, function (err) {
if (err) cancelled = true
done(err)
})
}))
}
})
})
}
})
}
}