-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip-mbcs.js
69 lines (64 loc) · 1.61 KB
/
unzip-mbcs.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
'use strict';
const fs = require('fs');
const AdmZip = require('adm-zip');
const iconv = require('iconv-lite');
const method2String = {
0: 'stored',
1: 'shrunk',
6: 'imploded',
8: 'deflated',
9: 'deflate64',
14: 'LZMA'
};
function fixZipFilename(filename, encoding) {
encoding = encoding || 'cp437';
return iconv.decode(filename, encoding);
}
function listSync(zipFilename, encoding) {
var zip = new AdmZip(zipFilename);
var results = zip.getEntries().map(function(x) {
return {
path: fixZipFilename(x.rawEntryName, encoding),
time: x.header.time,
size: x.header.size,
method: method2String[x.header.method] || 'unknown'
};
});
return results;
}
function extractSync(zipFilename, encoding, filters) {
var zip = new AdmZip(zipFilename);
var zipEntries = zip.getEntries();
if (filters && filters.length > 0) {
zipEntries.forEach(function(x) {
var path = fixZipFilename(x.rawEntryName, encoding);
var match = filters
.map(function(x) {
return path.startsWith(x);
})
.reduce(function(acc, cur) {
return (acc || cur);
}, false);
if (match) {
if (x.isDirectory) {
fs.mkdirSync(path);
} else {
fs.writeFileSync(path, zip.readFile(x));
}
}
});
} else {
zipEntries.forEach(function(x) {
var path = fixZipFilename(x.rawEntryName, encoding);
if (x.isDirectory) {
fs.mkdirSync(path);
} else {
fs.writeFileSync(path, zip.readFile(x));
}
});
}
}
module.exports = {
listSync: listSync,
extractSync: extractSync
};