NodeJS12 and Zlib
It works as it should be as follow index.js
const zlib = require('zlib');
const input = 'hello carl, how are you?';
zlib.deflate(input, function(err, buffer) {
if (!err) {
console.log(buffer.toString('base64'));
}
});
const buffer = Buffer.from('eJzT0yMAAGTvBe8=', 'base64');
zlib.unzip(buffer, function(err, buffer) {
if (!err) {
console.log(buffer.toString());
}
});
> node index.js
eJzLSM3JyVdITizK0VHIyC9XSCxKVajML7UHAGvnCIU=
.................................
Convert the callback mode to async await mode
const zlib = require('zlib');
const input = 'hello carl, how are you doing?';
(async () => {
const output = await new Promise((resolve, reject) => {
zlib.deflate(input, function(err, buffer) {
return void err ? reject(err) : resolve(buffer.toString('base64'))
})
});
console.log(output);
const buffer = Buffer.from(output, 'base64');
const resp = await new Promise((resolve, reject) => {
zlib.unzip(buffer, function(err, buffer) {
return void err ? reject(err) : resolve(buffer.toString())
});
});
console.log(resp);
})();
> node index.js
eJzLSM3JyVdITizK0VHIyC9XSCxKVajML1VIyc/MS7cHAKaqCrY=
hello carl, how are you doing?
Handle both gzip data or text data with base64
const zlib = require('zlib');
const input = 'Hello World';
const base64Input = Buffer.from("Hello World").toString('base64');
(async () => {
const outputZip = await new Promise((resolve, reject) => {
zlib.deflate(input, function(err, buffer) {
return void err ? reject(err) : resolve(buffer.toString('base64'))
})
});
console.log(outputZip);
console.log(base64Input);
//console.log(Buffer.from("Hello World").toString('base64'));
//console.log(Buffer.from("SGVsbG8gV29ybGQ=", 'base64').toString());
const rawInput = Buffer.from(base64Input, 'base64');
const resp = await new Promise((resolve, reject) => {
zlib.unzip(rawInput, function(err, buffer) {
return void err ? reject(err) : resolve(buffer ? buffer.toString() : rawInput.toString());
});
});
console.log(resp);
})();
> node index.js
eJzzSM3JyVcIzy/KSQEAGAsEHQ==
SGVsbG8gV29ybGQ=
Hello World
References:
https://millermedeiros.github.io/mdoc/examples/node_api/doc/zlib.html
https://stackoverflow.com/questions/12148948/how-do-i-ungzip-decompress-a-nodejs-requests-module-gzip-response-body
https://nodejs.org/docs/latest-v12.x/api/zlib.html
分享到:
相关推荐
nodejs 12,配合yarn使用 nodejs 12,配合yarn使用 nodejs 12,配合yarn使用 nodejs 12,配合yarn使用 nodejs 12,配合yarn使用
nodejs安装文件包,linux下的安装包亲!
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
Productive and Fun Web Development with NodeJS and Microsoft Developer Tools
官方离线安装包,测试可用。使用rpm -ivh [rpm完整包名] 进行安装
nodejs初级教程
这是一个vue+nodejs+express+mybatis够成的一个项目案例
NodeJS
nodejs 搭建oauth服务器的代码 Install nodejs and npm and then, simply run npm install and npm start. The server should now be running at http://localhost:3000.
NodeJS_and_ReactJS_Projects 基于NodeJS和ReactJS的项目
节点js NodeJS示例项目
,nodEjs ,nodEjs ,nodEjs ,nodEjs ,nodEjs ,nodEjs ,nodEjs ,nodEjs ,nodEjs
This book has complete source code with all required features of websites, registration, login, edit, delete and session ...Chapter 11 Downloading Entire Project and using it Chapter 12 About us
ECMAScript 6.0(以下简称 ES6)是 JavaScript 语言的下一代标准,已经在 2015 年 6 月正式发布了。它的目标,是使得 JavaScript 语言可以用来编写复杂的大型应用程序,成为企业...此文档整理nodejs中ES6的基本重点语法
nodejs安装及环境配置
based on nodejs as server and return a web
nodejs_and_typescript
资源名称:NodeJs技术经典文档资源目录:【】Node.js开发指南_中文正版【】nodejs开发指南pdf及源码及win安装程序【】nodejs手册中文【】NodeJs教程【】umav4simple【】七天学会NodeJS【】七天学会NodeJS【】深入浅...
Learn With Angular 4, Bootstrap, and NodeJS 英文epub 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者或csdn删除