【问题标题】:How to extract .rar files in node.js?如何在 node.js 中提取 .rar 文件?
【发布时间】:2015-11-09 15:28:07
【问题描述】:

我正在尝试在 Windows 8.1 中使用 node.js 提取 .rar 文件。有什么好办法吗?

提前致谢

【问题讨论】:

  • 我会参考以下回复stackoverflow.com/a/27450112/2824993
  • @cdurth 有没有其他不使用 Rar 的方法,Rar 是一个闭源软件?我已经尝试过了,但出现的另一个问题是:在我使用 http.get 下载 rar 文件并运行该代码后,它说我尝试提取的文件不是 rar 文件,而它显然是 rar 文件

标签: node.js module extract rar


【解决方案1】:
var Unrar = require('unrar'),
fs = require('fs'),
archive = new Unrar('t.rar');


archive.list(function(err, entries) {

for (var i = 0; i < entries.length; i++) {
    var name = entries[i].name;
    var type = entries[i].type
    if (type !== 'File') {
        fs.mkdirSync(name)
    }
}

for (var i = 0; i < entries.length; i++) {
    var name = entries[i].name;
    var type = entries[i].type;
    if (type !== 'File') {
        continue;
    }

    var stream = archive.stream(name);
    try {
        fs.writeFileSync(name, stream);
    } catch (e) {
        throw e;
    }
}
});

请检查unrar这可能会有所帮助

*此脚本在 Linux Ubuntu 上测试

【讨论】:

    【解决方案2】:

    RAR

    unrar-promise

     const unrarp = require('unrar-promise');   
          unrarp
          .extractAll('rar-file-path', 'extract-directory')
          .then(result => {
            cb(null, result);
          })
          .catch(err => {
            cb(err);
          });
    

    【讨论】:

      【解决方案3】:

      您也可以使用 DecompressZip 模块来提取 zip/rar 文件。Documentation and installation of Decompress-zip

      var DecompressZip = require('decompress-zip');
      var unzipper = new DecompressZip(filename)
      unzipper.on('error', function (err) {
      console.log('Caught an error');
      });
      
      unzipper.on('extract', function (log) {
      console.log('Finished extracting');
      });
      
      unzipper.on('progress', function (fileIndex, fileCount) {
      console.log('Extracted file ' + (fileIndex + 1) + ' of ' + fileCount);
      });
      
      unzipper.extract({
      path: 'some/path',
      filter: function (file) {
          return file.type !== "SymbolicLink";
      }
      });
      

      【讨论】:

      • 我没有看到任何提到在库中解压 rar 文件
      猜你喜欢
      • 1970-01-01
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-15
      • 2014-12-09
      • 2012-07-23
      • 1970-01-01
      相关资源
      最近更新 更多