简体   繁体   English

node.js中压缩文件夹到zip

[英]Comppresing folder to zip in node.js

I'm creating a discord bot in discord.js that saves messages on the server.我正在 discord.js 中创建一个 discord 机器人,它将消息保存在服务器上。 I would like to make it send a.zip compressed file from the saved messages folder.我想让它从保存的消息文件夹中发送 a.zip 压缩文件。 I know how to send attechment but I don't know how to add files from directory to this zip我知道如何发送 attechment 但我不知道如何将目录中的文件添加到此 zip

I tried this:我试过这个:

But files aren't appending in zip.但是文件没有附加到 zip 中。
Could someone please send fixed code?有人可以发送固定代码吗?
Thank you in advance for your help.预先感谢您的帮助。

var fs = require("fs");
var JSZip = require("jszip");
var zip = new JSZip();

zip
   .folder("data");
   .generateNodeStream({ type: 'nodebuffer', streamFiles: true })

  .pipe(fs.createWriteStream("logs.zip"))
   .on('finish', function () {
       console.log("out.zip written.");
   });

At the Discord.js Documentation in stable version, TextChannel reference has a method send that allow send remote and local files.稳定版本的Discord.js 文档中, TextChannel 参考有一个发送方法,允许发送远程和本地文件。

Sample code of documentation for send a local file:发送本地文件的文档示例代码:

// Send a local file
channel.send({
  files: [{
    attachment: 'entire/path/to/file.jpg',
    name: 'file.jpg'
    description: 'A description of the file'
  }]
})
  .then(console.log)
  .catch(console.log);

You can build a absolute path file with the path module and give as attachment value.您可以使用路径模块构建绝对路径文件并作为attachment值提供。

const path = require('path');
const ABSOLUTE_PATH_FILE = path.resolve(__dirname, "./../files/filename.zip");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM