【问题标题】:How to unzip a file requested with https如何解压缩使用 https 请求的文件
【发布时间】:2022-01-09 20:14:31
【问题描述】:

我正在尝试获取并解压缩位于此处的文件:https://donnees.roulez-eco.fr/opendata/jour

我尝试了一些东西和一些资源,包括this discussion,但我不知道我做错了什么。

该文件是一个 zip 文件,其中包含一个 XML 文件。我的目标是获取 XML 数据。以下是我正在尝试的代码,但不断收到此错误:

错误:标头检查不正确 在 Zlib.zlibOnError [as onerror] (zlib.js:181:17) { 错误号:-3, 代码:'Z_DATA_ERROR' }

const https = require('https');
const zlib = require('zlib');

// https get file from url
const getFile = url => {
  const httpOptions = {
    headers: {
      'accept-encoding': 'gzip,deflate',
    },
  };

  return new Promise((resolve, reject) => {
    https
      .get(url, httpOptions, res => {
        let data = [];
        res.on('data', chunk => {
          data.push(chunk);
        });
        res.on('end', () => {
          console.log(`headers: ${JSON.stringify(res.headers, null, 2)}`);
          resolve(Buffer.concat(data));
        });
      })
      .on('error', e => {
        console.log('https.get error');
        reject(e);
      });
  });
};

// unzip buffer from getFile
const unzipBuffer = buffer => {
  console.log(`trying to unzip blob of size ${buffer.length}`);
  return new Promise((resolve, reject) => {
    zlib.gunzip(buffer, (err, buffer) => {
      if (err) {
        console.log('unzipping error');
        reject(err);
      } else {
        console.log('unzipping success');
        resolve(buffer);
      }
    });
  });
};

(async () => {
  const timer = new Timer();
  try {
    const file = await getFile('https://donnees.roulez-eco.fr/opendata/jour');
    const unzipped = await unzipBuffer(file);
    console.log(`unzipped file of size ${unzipped.length}`);
  } catch (err) {
    console.log(err);
  } 
})();

另外,这些是我得到的标题:

headers: {
  "date": "Fri, 03 Dec 2021 12:51:39 GMT",
  "content-type": "application/zip",
  "content-length": "1201261",
  "content-disposition": "attachment;filename=\"PrixCarburants_quotidien_20211202.zip\"",
  "last-modified": "Thu, 02 Dec 2021 23:24:14 GMT",
  "content-security-policy": "default-src https: 'unsafe-eval' 'unsafe-inline'; object-src https: ; child-src https: platform.twitter.com; img-src https: data:",
  "strict-transport-security": "max-age=34560000; includeSubDomains",
  "x-request-id": "246579460",
  "cache-control": "max-age=300",
  "x-cdn-pop": "rbx1",
  "x-cdn-pop-ip": "51.254.41.128/25",
  "x-cacheable": "Cacheable",
  "accept-ranges": "bytes",
  "connection": "close"
}

非常感谢所有帮助

【问题讨论】:

    标签: javascript node.js https zlib


    【解决方案1】:

    看起来你有一个 zip 文件。 zlib 只会解码 gzip 或 zlib 流。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-07-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多