-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathunzip.mjs
83 lines (80 loc) · 2.79 KB
/
unzip.mjs
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
import * as yauzl from "yauzl";
import * as path from "node:path";
import * as fs from "node:fs";
import * as fsPromises from "node:fs/promises";
var unzip=(filename,contentroot,dest,filter=()=>true)=>new Promise(
(resolve,reject)=>yauzl.open(filename, {lazyEntries: true}, async function(ziperror, zipfile) {
if (ziperror) reject(ziperror);
let ncroot=path.normalize(contentroot);
let contentrootlength=ncroot.split(path.sep).length;
if(ncroot=="."){
ncroot="";
contentrootlength=0;
}
zipfile.readEntry();
zipfile.on("entry", function(entry) {
if (/\/$/.test(entry.fileName)) {
zipfile.readEntry();
} else {
if(!path.normalize(entry.fileName).startsWith(ncroot)){
zipfile.readEntry();
return;
}
if(!filter(entry.fileName)){
zipfile.readEntry();
return;
}
const pathComponents = path.normalize(entry.fileName).split(path.sep);
const fname=path.join(...pathComponents.slice(contentrootlength));
// file entry
zipfile.openReadStream(entry, function(readerror, readStream) {
if (readerror) throw readerror;
readStream.on("end", function() {
zipfile.readEntry();
});
fsPromises.mkdir(path.join(dest,path.dirname(fname)), { recursive: true }).then(()=>{
var stream=fs.createWriteStream(path.join(dest,fname));
readStream.pipe(stream);
});
});
}
}).once("end",()=>{
zipfile.close();
resolve();
});
})
);
var unzipStreams=(filename,contentroot,dest,filter=()=>true)=>new Promise(
(resolve,reject)=>{
const entries=new Map();
// lazyEntries isn't needed because the caller opens the streams
return yauzl.open(filename, async function(ziperror, zipfile) {
if (ziperror) reject(ziperror);
let ncroot=path.normalize(contentroot);
let contentrootlength=ncroot.split(path.sep).length;
if(ncroot=="."){
ncroot="";
contentrootlength=0;
}
zipfile.readEntry();
zipfile.on("entry", function(entry) {
if (!(/\/$/.test(entry.fileName))) {
if(!path.normalize(entry.fileName).startsWith(ncroot)){
return;
}
if(!filter(entry.fileName)){
return;
}
const pathComponents = path.normalize(entry.fileName).split(path.sep);
const fname=path.join(...pathComponents.slice(contentrootlength));
// a function that when called resolves to a read stream
entries.set(fname,()=>util.promisify(zipfile.openReadStream)(entry));
}
}).once("end",()=>{
//zipfile.close(); // don't close until all files have been read
resolve({zipfile,entries});
});
})
}
);
export {unzip,unzipStreams};