-
Notifications
You must be signed in to change notification settings - Fork 0
/
unzip.js
66 lines (60 loc) · 1.98 KB
/
unzip.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
/*
* @Author: iDzeir
* @Date: 2018-09-30 13:39:38
* @Last Modified by: iDzeir
* @Last Modified time: 2018-10-10 12:31:38
*/
const path = require('path');
const fs = require('fs');
const crypto = require('crypto');
const { path: folder, pwd, output } = require('./commons/cmder');
const log = require('./commons/log');
async function unpack() {
log('解压文件:', path.join(__dirname, folder));
const begin = Date.now();
const files = await paseFiles(folder);
log('文件数:', files.size)
await write(files);
return Date.now() - begin;
}
async function paseFiles(fileUrl) {
const decipher = crypto.createDecipher('aes192', pwd);
const zipstream = decipher.update(fs.readFileSync(path.join(__dirname, fileUrl)));
let offset = 0;
const SIZE_BYTE = 4;
const files = new Map();
while (true) {
const urlsize = zipstream.readUInt32BE(offset);
offset += SIZE_BYTE;
const url = zipstream.slice(offset, offset + urlsize);
offset += urlsize;
const fileSize = zipstream.readUInt32BE(offset);
offset += SIZE_BYTE;
const file = zipstream.slice(offset, offset + fileSize);
offset += fileSize;
files.set(url.toString(), file);
if (offset >= zipstream.byteLength) {
log('压缩文件解析完毕!!!');
break;
}
}
return files;
}
async function write(files) {
for (let [url, file] of files) {
await new Promise((res, rej) => {
const filePath = path.join(output, url);
require('./commons/mkdir')(path.parse(filePath).dir);
const filestream = fs.createWriteStream(filePath);
filestream.write(file, err => {
if (err) {
rej(err);
return;
}
filestream.close();
res();
});
});
}
}
unpack().then(spend => log('解压完毕!!!耗时:', require('./commons/time')(spend)));