1 | const fs = require('fs')
|
2 | const path = require('path')
|
3 | const zlib = require('zlib')
|
4 |
|
5 | module.exports = file => new Promise((resolve, reject) => {
|
6 | let gzFile = `${file}.gz`
|
7 |
|
8 | const filePath = path.resolve(file)
|
9 | const gzFilePath = path.resolve(gzFile)
|
10 |
|
11 | let gzip = zlib.createGzip()
|
12 |
|
13 | const inputStream = fs.createReadStream(filePath)
|
14 | const outStream = fs.createWriteStream(gzFilePath)
|
15 |
|
16 | inputStream.pipe(gzip).pipe(outStream)
|
17 |
|
18 | outStream.on('finish', () => {
|
19 | fs.unlinkSync(filePath)
|
20 | resolve()
|
21 | })
|
22 | })
|