0

I am working on a project were i need to extract every file (xml) in an zip to an array of strings. I am going parse trough the array later on. I have tried so many imports like jszip, yauzl, unzipper, adm-zip, node-stream-zip and so on... But i cant manage to do it. Can any of you guys help me?

const conf = {
  headers: {
    "Content-Type": "application/zip",
  },
  auth: {
    username: "username",
    password: "password",
  },
};

try {
  const res = await axios.get("url", conf);
  zipFiles = []

  <!--- Code to push files in zip to array .....--->

  console.log(zipFiles)
  /*
  Return something like:
  [
  "<?xml versi...",
  "<?xml versi...",
  "<?xml versi..."
  ]
  */

} catch (e) {
  console.log(e);
}

This link kinda answered the problem: Download zip with axios and unzip with adm-zip in memory (electron app) But i get Error: Invalid filename since AdmZip needs a Buffer while the zip is a utf8 string

Sondge
  • 72
  • 4
  • Would nodejs zlib work for you? https://nodejs.org/dist/latest-v18.x/docs/api/zlib.html – Felix Oct 16 '22 at 03:34
  • Are you able to share the API from where you are downloading the files? You might be able to save the files first and then read them, if you don't want to work with streams – Shaunak Oct 16 '22 at 03:50
  • No sorry, I can't share the API as this is work. I'm could downnload it yes. But this code is going to be uploaded to a server as a ingestor. So I want to prevent the creation of a file. – Sondge Oct 16 '22 at 09:36
  • please show what you have tried, listing a bunch of libs is not trying, do this: https://stuk.github.io/jszip/documentation/howto/read_zip.html – Lawrence Cherone Oct 16 '22 at 12:53

1 Answers1

0

I found a solution, since i know AdmZip fixes the problem if i can convert the data to a Buffer. So my sulution was add responseType: "arraybuffer",

  const conf = {
    headers: {
      "Content-Type": "application/zip",
    },
    responseType: "arraybuffer",
    auth: {
      username: "username",
      password: "password",
    },
  };

  try {
    const res = await axios.get("url", conf);
    zipFiles = []

    <!--- Code to push files in zip to array .....--->
    const zip = new AdmZip(data);
    const zipEntries = zip.getEntries();
    for (const zipEntry of zipEntries) {
      files.push(zipEntry.getData().toString("utf8"));
    }

    console.log(zipFiles)
    /*
    Return something like:
    [
    "<?xml versi...",
    "<?xml versi...",
    "<?xml versi..."
    ]
    */

  } catch (e) {
    console.log(e);
  }
Sondge
  • 72
  • 4