tar.js
1.31 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
/**
* zip/unzip folder
*
* @author: jf<jeff.jiang@yoho.com>
* @date: 2016/5/20
*/
'use strict';
import zlib from 'zlib';
import tar from 'tar';
import fstream from 'fstream';
import fsp from 'fs-promise';
import fs from 'fs';
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 {
fstream.Reader(readCfg) /* Read the source directory */
.pipe(tar.Pack()) /* Convert the directory to a .tar file */
.pipe(zlib.Gzip()) /* Compress the .tar file */
.pipe(fstream.Writer({
'path': `${dist}`
}));
reslove();
} catch (e) {
reject(e);
}
});
})
}
}
export default Tar;