【发布时间】:2017-08-07 16:41:20
【问题描述】:
我想在 Node.js 中解压缩 gzip 文件我尝试了 [一些] 包,但没有任何效果。能否提供一个可以在 Node.js 中解压 gzip 文件的示例代码包?
【问题讨论】:
-
你试过什么包?有什么问题或错误? zlib.Gunzip 可以做到。
-
你能告诉我zlib.gunzip的代码吗?因为我找不到zlib.gunzip的例子
我想在 Node.js 中解压缩 gzip 文件我尝试了 [一些] 包,但没有任何效果。能否提供一个可以在 Node.js 中解压 gzip 文件的示例代码包?
【问题讨论】:
我建议使用zlib.Gunzip。
函数原型是zlib.Gunzip(buf, callback)。第一个参数是您要提取的raw archive data as a buffer,第二个参数是接受两个参数(结果和错误)的回调。
一个实现是:
zlib.Gunzip(raw_data, function (error, result) {
if (error) throw error;
// Access data here through result as a Buffer
})
【讨论】:
raw_data的?
gunzip-file 节点包运行良好!
做:
npm install gunzip-file
然后:
'use strict'
const gunzip = require('gunzip-file')
// 'sitemap.xml.gz' - source file
// 'sitemap.xml' - destination file
// () => { ... } - notification callback
gunzip('sitemap.xml.gz', 'sitemap.xml', () => {
console.log('gunzip done!')
})
最后,在你的 shell 中运行 Node。
【讨论】:
zlib 模块的一部分,从第一天起就有效地成为 Node 的一部分。安装一个单独的包只是为了做 Node 已经可以做的事情有点傻 =)
zlib 在 v0.5.8 中被添加到 Node 中,该版本于 2011 年底发布。那是很多很多年之前的 很多 年这个答案已发布。无论是 2012 年、2017 年还是 2022 年,都没有(现在是)必要的第三方软件包 =)
您可以使用tar.gz npm 包来解决这个问题。
编写以下方法将其提取到特定路径:
targz().extract('/bkp/backup.tar.gz', '/home/myuser')
.then(function(){
console.log('Job done!');
})
.catch(function(err){
console.log('Something is wrong ', err.stack);
});
更多详情可以关注link
【讨论】: