node.js - 逐行读取gzip流

标签 node.js gzip zlib

我有一个压缩的 gzip 文件,我想逐行阅读。

var fs = require('fs')
var zlib = require('zlib')
var gunzip = zlib.createGunzip()
var inp = fs.createReadStream('test.gz')
var n = 0

var lineProcessing = function (err, data) {
    if (!err) {
        n += 1
        console.log ("line: " + n)
        console.log (data.toString())
    }
}

inp
  .on('data', function (chunk) {
      zlib.gunzip (chunk, lineProcessing)
  })
  .on('end', function () {
    console.log ('ende');
  });

我想我需要为 zlib.createGunzip 设置一个 block 大小,我只在下一个 \n 之前读取它。但是如何动态确定呢?

最佳答案

readline 可能更容易使用为此:

const fs       = require('fs');
const zlib     = require('zlib');
const readline = require('readline');

let lineReader = readline.createInterface({
  input: fs.createReadStream('test.gz').pipe(zlib.createGunzip())
});

let n = 0;
lineReader.on('line', (line) => {
  n += 1
  console.log("line: " + n);
  console.log(line);
});

关于node.js - 逐行读取gzip流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38074288/

相关文章:

gzip - Moovweb - 解压缩 gzipped outgoing_response.http 内容?

c++ - zlib 对结构中 float 的压缩

c++ - DEFLATED 数据将始终大于未压缩的输入数据

windows - 使用 Windows 8 SDK 为 Windows 7 编译

node.js - 使用 npm 安装时找不到带有 node-gyp 重建的 python 可执行文件

python - 从 Node 脚本激活 Python virtualenv

java - Gzip 不工作?

node.js - 动态运行 Mocha 测试

javascript - 如何在javascript函数中执行python代码(.py文件)?

iphone - 如何跟踪 gzip 编码的 Web 内容的下载进度?