【问题标题】:How to convert files to .RAR file node js如何将文件转换为 .RAR 文件节点 js
【发布时间】:2018-11-07 23:22:04
【问题描述】:

在我的项目中,我有一个项目表。每个项目都有一个下载pdf文件的栏目。现在我希望能够下载所有文件并创建一个 .rar 文件。有一个下载单个文件的代码:

routes.js

app.get('/api/download/archive/:filename', function(req,res){
    res.download("public/uploads/"+req.params.filename, req.params.filename);
}) 

存档.js

$scope.downloadPdf = function(obj){
    $http.get('api/download/archive/'+obj.Documentation)
    .success(function(data){
        window.open('api/download/archive/'+obj.Documentation)
    });
}

【问题讨论】:

标签: angularjs node.js rar


【解决方案1】:

不幸的是,RAR 是一个闭源软件。因此,创建存档的唯一方法是安装名为rar 的命令行实用程序,然后在子进程中使用rar a 命令来压缩文件。

要在 Mac 上安装 rar,我必须运行 brew install homebrew/cask/rar。你可以找到其他平台的安装说明here

安装后你可以像这样使用child_process

const { exec } = require('child_process');
const { promisify } = require('util');
const fs = require('fs');
const path = require('path');

// Promisify `unlink` and `exec` functions as by default they accept callbacks
const unlinkAsync = promisify(fs.unlink);
const execAsync = promisify(exec);

(async () => {
    // Generating a different name each time to avoid any possible collisions
    const archiveFileName = `temp-archive-${(new Date()).getTime()}.rar`;
    // The files that are going to be compressed.
    const filePattern = `*.jpg`;

    // Using a `rar` utility in a separate process
    await execAsync(`rar a ${archiveFileName} ${filePattern}`);

    // If no error thrown the archive has been created
    console.log('Archive has been successfully created');

    // Now we can allow downloading it

    // Delete the archive when it's not needed anymore
    // await unlinkAsync(path.join(__dirname, archiveFileName));

    console.log('Deleted an archive');
})();

为了运行示例,请将一些.jpg 文件放入项目目录中。

PS:如果您选择不同的存档格式(例如 .zip),您可以使用 archiver 之类的东西。这可能允许您创建一个 zip 流并将其通过管道直接响应。因此,您无需在磁盘上创建任何文件。 但这是一个不同的问题。

【讨论】:

  • 感谢您的回答(示例不起作用)。我能够使用归档器 (archiverjs.com/docs) 创建一个 .rar 文件。我更改了 .zip ->.rar 并创建了一个 rar 文件。
  • 我的示例创建了一个存档,然后立即将其删除。 :) 请注释掉 unlink 行。另外请确保已安装rar 并且目录中有一些 .jpg 文件。请让我知道它是否有效。 @维塔利
  • @Vitali 也很可能archiver 使用了 .zip 算法。该文件只是名为 .rar,但它仍然是一个 zip 存档。
  • @Antonio Narkevich 我找不到 windows 的安装说明
  • @Vitali 如果您只是将 WinRAR 放入,然后在命令行中尝试 rar,它很可能会出现。你能试试吗?不过会做一些谷歌搜索。
【解决方案2】:

试试WinrarJs
该项目允许您Create a RAR fileRead a RAR fileExtract a RAR archive

这是来自 GitHub 的示例:

/* Create a RAR file */

var winrar = new Winrar('/path/to/file/test.txt');
// add more files
winrar.addFile('/path/to/file/test2.txt');
// add multiple files
winrar.addFile(['/path/to/file/test3.txt', '/path/to/file/test4.txt']);

// set output file
winrar.setOutput('/path/to/output/output.rar');

// set options
winrar.setConfig({
    password: 'testPassword',
    comment: 'rar comment',
    volumes: '10',  // split volumes in Mega Byte
    deleteAfter: false, // delete file after rar process completed
    level: 0 // compression level 0 - 5
});

// archiving file
winrar.rar().then((result) => {
    console.log(result);
}).catch((err) => {
    console.log(err);
}

【讨论】:

    猜你喜欢
    • 2018-10-13
    • 2016-08-02
    • 2020-10-11
    • 1970-01-01
    • 1970-01-01
    • 2017-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多