无法使用zlib模块解压缩压缩文件

时间:2020-06-06 09:20:49

标签: node.js zlib

我正在尝试使用节点内置模块zlib从压缩数据中解压缩文件,由于某种原因,我无法解压缩它,出现如下错误:

Error: incorrect header check
test.js:53
No debug adapter, can not send 'variables'

我正在尝试的代码如下:

var zlib = require('zlib');
var fs = require('fs');
var filename = './Divvy_Trips_2019_Q2.zip';

var str1 = fs.createReadStream(filename);

var gzip = zlib.createGunzip();

str1.pipe(gzip).on('data', function (data) {
    console.log(data.toString());
}).on('error', function (err) {
    console.log(err);
});

压缩数据的URL如下:Divvy_Trips_2019_Q2.zip

1 个答案:

答案 0 :(得分:1)

GZip(.gz)和ZIP(.zip)是不同的格式。您需要一个处理ZIP文件的库,例如yauzl

// https://github.com/thejoshwolfe/yauzl/blob/master/examples/dump.js

const yauzl = require("yauzl");

const path = "./Divvy_Trips_2019_Q2.zip";

yauzl.open(path, function(err, zipfile) {
  if (err) throw err;
  zipfile.on("error", function(err) {
    throw err;
  });
  zipfile.on("entry", function(entry) {
    console.log(entry);
    console.log(entry.getLastModDate());
    if (/\/$/.exec(entry)) return;
    zipfile.openReadStream(entry, function(err, readStream) {
      if (err) throw err;
      readStream.pipe(process.stdout);
    });
  });
});