tar.js
1.65 KB
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
/**
* zip/unzip folder
*
* @author: jf<jeff.jiang@yoho.com>
* @date: 2016/5/20
*/
'use strict';
const zlib = require('zlib');
const tar = require('tar');
const fstream = require('fstream');
const fsp = require('fs-promise');
const fs = require('fs');
const md5File = require('md5-file');
class Tar {
static gzip(target, dist) {
console.log(`gzip ${target} to ${dist}`);
return fsp.stat(target).then((stats) => {
let readCfg = {
path: target
};
if (!stats) {
return Promise.reject(`gzip target file is not exists: ${target}`);
} else if (stats.isDirectory()) {
readCfg.type = 'Directory';
} else if (stats.isFile()) {
readCfg.type = 'File';
}
return new Promise((reslove, reject) => {
try {
let packer = tar.Pack();
let wirte = fstream.Writer({
'path': `${dist}`
}).on('close', () => {
let filecode = md5File.sync(dist);
console.log(filecode);
reslove(filecode);
});
fstream.Reader(readCfg) /* Read the source directory */
.pipe(packer) /* Convert the directory to a .tar file */
.pipe(zlib.Gzip()) /* Compress the .tar file */
.pipe(wirte);
} catch (e) {
reject(e);
}
});
})
}
}
module.exports = Tar;