-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompressor.js
32 lines (31 loc) · 1.01 KB
/
compressor.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
const fs = require('fs');
const path = require('path');
const zlib = require('zlib');
module.exports = {
compress: function (file, cb) {
const gzip = zlib.createGzip();
const inp = fs.createReadStream(file);
const gzFile = path.join(path.dirname(file), `${path.basename(file, path.extname(file))}.gz`);
const out = fs.createWriteStream(gzFile);
inp.pipe(gzip).pipe(out)
.on('error', (err) => {
cb(err, undefined);
})
.on('finish', () => {
cb(undefined, gzFile);
});
},
decompress: function (gzFile, cb) {
const unzip = zlib.createUnzip();
const inp = fs.createReadStream(gzFile);
const file = path.join(path.dirname(gzFile), `${path.basename(gzFile, '.gz')}.txt`);
const out = fs.createWriteStream(file);
inp.pipe(unzip).pipe(out)
.on('error', (err) => {
cb(err, undefined);
})
.on('finish', () => {
cb(undefined, out);
});
},
};