如何在NodeJs中下载和解压缩内存中的zip文件? [英] How to download and unzip a zip file in memory in NodeJs?

查看:912
本文介绍了如何在NodeJs中下载和解压缩内存中的zip文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从互联网上下载一个zip文件并将其解压缩到内存而不保存到临时文件。我怎么能这样做?

I want to download a zip file from the internet and unzip it in memory without saving to a temporary file. How can I do this?

这是我试过的:

var url = 'http://bdn-ak.bloomberg.com/precanned/Comdty_Calendar_Spread_Option_20120428.txt.zip';

var request = require('request'), fs = require('fs'), zlib = require('zlib');

  request.get(url, function(err, res, file) {
     if(err) throw err;
     zlib.unzip(file, function(err, txt) {
        if(err) throw err;
        console.log(txt.toString()); //outputs nothing
     });
  });


建议,我尝试使用adm-zip库和我仍然不能使这项工作:

As, suggested, I tried using the adm-zip library and I still cannot make this work:

var ZipEntry = require('adm-zip/zipEntry');
request.get(url, function(err, res, zipFile) {
        if(err) throw err;
        var zip = new ZipEntry();
        zip.setCompressedData(new Buffer(zipFile.toString('utf-8')));
        var text = zip.getData();
        console.log(text.toString()); // fails
    });


推荐答案

您需要一个可以处理缓冲区的库。最新版本的 adm-zip 将执行:

You need a library that can handle buffers. The latest version of adm-zip will do:

npm install adm-zip

我的解决方案使用 http.get 方法,因为它返回缓冲区块。

My solution uses the http.get method, since it returns Buffer chunks.

代码:

var file_url = 'http://notepad-plus-plus.org/repository/7.x/7.6/npp.7.6.bin.x64.zip';

var AdmZip = require('adm-zip');
var http = require('http');

http.get(file_url, function(res) {
  var data = [], dataLen = 0; 

  res.on('data', function(chunk) {
    data.push(chunk);
    dataLen += chunk.length;

  }).on('end', function() {
    var buf = Buffer.alloc(dataLen);

    for (var i = 0, len = data.length, pos = 0; i < len; i++) { 
      data[i].copy(buf, pos); 
      pos += data[i].length; 
    } 

    var zip = new AdmZip(buf);
    var zipEntries = zip.getEntries();
    console.log(zipEntries.length)

    for (var i = 0; i < zipEntries.length; i++) {
      if (zipEntries[i].entryName.match(/readme/))
        console.log(zip.readAsText(zipEntries[i]));
    }
  });
});

想法是创建一个缓冲区数组,并在最后将它们连接成一个新缓冲区。这是因为缓冲区无法调整大小。

The idea is to create an array of buffers and concatenate them into a new one at the end. This is due to the fact that buffers cannot be resized.

更新

这是一个更简单的解决方案,它使用请求模块通过设置 encoding:null 来获取缓冲区中的响应选项。它还遵循重定向并自动解析http / https。

This is a simpler solution that uses the request module to obtain the response in a buffer, by setting encoding: null in the options. It also follows redirects and resolves http/https automatically.

var file_url = 'https://github.com/mihaifm/linq/releases/download/3.1.1/linq.js-3.1.1.zip';

var AdmZip = require('adm-zip');
var request = require('request');

request.get({url: file_url, encoding: null}, (err, res, body) => {
  var zip = new AdmZip(body);
  var zipEntries = zip.getEntries();
  console.log(zipEntries.length);

  zipEntries.forEach((entry) => {
    if (entry.entryName.match(/readme/i))
      console.log(zip.readAsText(entry));
  });
});

响应的正文是一个缓冲区可以直接传递给 AdmZip ,简化整个过程。

The body of the response is a buffer that can be passed directly to AdmZip, simplifying the whole process.

这篇关于如何在NodeJs中下载和解压缩内存中的zip文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆