在内存中下载带有axios的zip文件和带有adm-zip的解压缩文件(电子应用程序) [英] Download zip with axios and unzip with adm-zip in memory (electron app)

查看:63
本文介绍了在内存中下载带有axios的zip文件和带有adm-zip的解压缩文件(电子应用程序)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要下载带有 axios 的文件,并将其解压缩到电子应用程序的内存中.

我读了一些SO线程(

I need to download a file with axios and unzip it in memory in an electron app.

I read in some SO threads (e.g.), that adm-zip supports byte buffer constructor, but I can not see this in the docs. When I extract the content, it behaves like the array is empty, but it is not. It just does create a file and does not throw any errors I do not want to use request, as the api is marked deprecated. My code is this:

const axios = require("axios");
const AdmZip = require('adm-zip');
   
const url = "http://update-service.test.w3champions.com/api/maps";
const body = await axios.get(url, {
    responseType: 'arraybuffer'
});
const data = body.data;
const zip = new AdmZip(data);
zip.extractAllTo(to, true);

I feel super stupid, because I had it one time working and then changed something and now I do not seem to find the error again :/ I sadly did not commit the working state...

edit: So, we figured it out: Electron does some weird stuff that returns an Array Buffer instead of a Buffer, that adm-zip would need. As I am lazy added the package arraybuffer-to-buffer and now the code works:

const arrayBufferToBuffer = window.require('arraybuffer-to-buffer');
const url = `${this.updateUrl}api/${fileName}?ptr=${this.isTest}`;
const body = await axios.get(url, {
    responseType: 'arraybuffer'
});

const buffer = arrayBufferToBuffer(body.data);
const zip = new AdmZip(buffer);
zip.extractAllTo(to, true);

解决方案

check the typeof data, maybe its not buffer.

Adm implementation: https://github.com/cthackers/adm-zip/blob/master/adm-zip.js

这篇关于在内存中下载带有axios的zip文件和带有adm-zip的解压缩文件(电子应用程序)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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