var zlib = require("zlib");
var input = '...............text..............';
//inflate 充气    deflate压缩
//gzip/gunzip:标准gzip压缩.
//deflate/inflate:基于Huffman编码的标准deflate压缩算法。
//deflateRaw/inflateRaw:针对原始缓冲区的deflate压缩算法。

zlib.deflate(input, function (err, buffer) {
    if (!err) {
        //压缩后不可识别
        console.log("deflate (%s): ", buffer.length, buffer.toString('base64'));
        //解压
        zlib.inflate(buffer, function (err, buffer) {
            if (!err) {
                console.log("inflate (%s): ", buffer.length, buffer.toString());
            }
        });
    }
});


zlib.deflateRaw(input, function (err, buffer) {
    if (!err) {
        console.log("deflateRaw (%s): ", buffer.length, buffer.toString('base64'));
        zlib.inflateRaw(buffer, function (err, buffer) {
            if (!err) {
                console.log("inflateRaw (%s): ", buffer.length, buffer.toString());
            }
        });
    }
});


zlib.gzip(input, function (err, buffer) {
    if (!err) {
        console.log("gzip (%s): ", buffer.length, buffer.toString('base64'));
        zlib.gunzip(buffer, function (err, buffer) {
            if (!err) {
                console.log("gunzip (%s): ", buffer.length, buffer.toString());
            }
        });
    }
});
const fs = require('fs');
const zlib = require('zlib');
const path = require('path');
function gzip($src) {
    fs.stat($src, function (err, stats) {
        if (stats.isFile()) {
            let rs = fs.createReadStream($src);
            //zlib.createGzip()创建一个gzip转换流,是一个可读可写流。
            //通过管道将数据读取出来写入gzip流,然后又通过管道写入一个文件流中
            $dst = path.join(__dirname, path.basename($src) + '.gz');
            rs.pipe(zlib.createGzip()).pipe(fs.createWriteStream($dst));
        }
    });
}
function ungzip($src) {
    fs.stat($src, function (err, stats) {
        if (stats.isFile()) {
            let rs = fs.createReadStream($src);
            //zlib.createGunzip()创建一个gunzip转换流
            $dst = path.join(__dirname, path.basename($src, '.gz'));
            rs.pipe(zlib.createGunzip()).pipe(fs.createWriteStream($dst));
        }
    });
}

//压缩文件
//gzip('./1.txt');
//解压文件
//ungzip('./1.txt.gz');
var zlib = require("zlib");
var input = '...............text..............';
//inflate 充气    deflate压缩
//gzip/gunzip:标准gzip压缩.
//deflate/inflate:基于Huffman编码的标准deflate压缩算法。
//deflateRaw/inflateRaw:针对原始缓冲区的deflate压缩算法。

zlib.deflate(input, function (err, buffer) {
    if (!err) {
        //压缩后不可识别
        console.log("deflate (%s): ", buffer.length, buffer.toString('base64'));
        //解压
        zlib.inflate(buffer, function (err, buffer) {
            if (!err) {
                console.log("inflate (%s): ", buffer.length, buffer.toString());
            }
        });
    }
});


zlib.deflateRaw(input, function (err, buffer) {
    if (!err) {
        console.log("deflateRaw (%s): ", buffer.length, buffer.toString('base64'));
        zlib.inflateRaw(buffer, function (err, buffer) {
            if (!err) {
                console.log("inflateRaw (%s): ", buffer.length, buffer.toString());
            }
        });
    }
});


zlib.gzip(input, function (err, buffer) {
    if (!err) {
        console.log("gzip (%s): ", buffer.length, buffer.toString('base64'));
        zlib.gunzip(buffer, function (err, buffer) {
            if (!err) {
                console.log("gunzip (%s): ", buffer.length, buffer.toString());
            }
        });
    }
});

 

posted on 2020-11-06 18:11  qqhfeng16  阅读(169)  评论(0编辑  收藏  举报