Search code examples
javascriptnode.jszip

I can't extract an .zip file


I am trying to download an .zip file from discord and extracting using decompress package, but it not return any errors and don't extract the package. (The file is saving and downloading correctly)

const decompress = require('decompress');
client.on("message", (msg) => {
  if (msg.author.id === process.env.REIS_ID) {
    if (msg.channel.id === config.channel_commit) {
      if (config.file_status === 0) {
        if (msg.attachments.first()) {
          download_files(msg.attachments.first().url).then(res => {
            console.log(res);    
          });
        }
      } else {
        msg.reply("Upload ainda em processo.");
      }
    }
  }
});
const download_files = async (url) => {
  const stream = got.stream(url);
  const file_ext = FileType.fromStream(stream).then(async (res) => {
    got
      .stream(url)
      .pipe(
        fs.createWriteStream("./testes/a/new." + res.ext, { overwrite: true})
      );
    console.log(res.ext);
    if (res.ext === "zip") {
      console.log("zip file");
        const files = await decompress("./testes/a/new.zip", "./testes/a/new/");
    }
  });
};

Solution

  • Solution: I download the unzip module from npm and make that the zip file is not saved to the folder, the request saves directly the extracted files.

    Code:

    
    client.on("message", (msg) => {
      if (msg.author.id === process.env.REIS_ID) {
        if (msg.channel.id === config.channel_commit) {
            if (msg.attachments.first()) {
              let res = download_files(msg.attachments.first().url);
            }
        }
      }
    });
    
    
    const download_files = (url) => {
      const stream = got.stream(url);
      const file_ext = FileType.fromStream(stream).then(async (res) => {
        got
          .stream(url)
        console.log(res.ext);
        if (res.ext === "zip") {
            got.stream(url).pipe(unzip.Extract({ path: "./testes/a/new/" }));
        } 
      });
    };