#javascript #node.js #zip
Вопрос:
Я пытаюсь загрузить zip-файл из discord и извлечь его с помощью пакета распаковки, но он не возвращает никаких ошибок и не извлекает пакет . (Файл сохраняется и загружается правильно)
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/");
}
});
};
Ответ №1:
Решение: Я загружаю модуль распаковки из npm и делаю так, чтобы zip-файл не сохранялся в папке, запрос сохраняет непосредственно извлеченные файлы.
Код:
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/" }));
}
});
};