-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzip.js
44 lines (41 loc) · 1.38 KB
/
zip.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
/*
* @Author: iDzeir
* @Date: 2018-09-28 16:07:44
* @Last Modified by: iDzeir
* @Last Modified time: 2018-10-10 12:31:17
*/
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');
const rootURL = path.join(__dirname, folder);
async function zip() {
const begin = Date.now();
log('打包目录:', rootURL);
const files = await require('./commons/readdir')(rootURL, rootURL);
log('包含文件:', files.size, '个');
const buffers = await require('./commons/files2buffer')(files);
log('总大小:', (buffers.byteLength / 1024 / 1024).toFixed(2), 'm');
const outdir = path.parse(output).dir;
log('文件输出目录:', outdir);
require('./commons/mkdir')(outdir);
await new Promise((res, rej) => {
const clipher = crypto.createCipher('aes192', pwd);
const zipstream = fs.createWriteStream(output);
zipstream.write(clipher.update(buffers), error => {
if (error) {
rej(error);
return;
}
zipstream.close();
res();
});
});
return Date.now() - begin;
}
zip()
.then(spend => {
log('打包完成!!!耗时:', require('./commons/time')(spend));
})
.catch(reason => console.log(reason));