如何在node.js中提取.rar文件?

3
我正在尝试在Windows 8.1中使用node.js提取.rar文件。有没有好的方法可以做到这一点?
提前致谢。

我会参考以下回答 https://dev59.com/mWEi5IYBdhLWcg3wWLAS#27450112 - cdurth
1
@cdurth有没有其他不使用Rar的方法,因为它是一个闭源软件?我已经尝试过这个方法,但又出现了另一个问题:在使用http.get下载rar文件后运行代码时,它显示我尝试提取的文件不是rar文件,而实际上它明明是一个rar文件。 - Mr Cold
5个回答

4

node-unrar-js 是一个基于原始源代码的JavaScript(确切地说是TypeScript)Rar解压器。与Unrar不同,它不需要在系统上安装外部软件。

我使用以下代码成功在Linux上提取了一个.rar文件。

import { createExtractorFromFile } from 'node-unrar-js'

async function extractRarArchive(file, destination) {
  try {
    // Create the extractor with the file information (returns a promise)
    const extractor = await createExtractorFromFile({
      filepath: file,
      targetPath: destination
    });

    // Extract the files
    [...extractor.extract().files];
  } catch (err) {
    // May throw UnrarError, see docs
    console.error(err);
  }
}

// Files are put directly into the destination
// The full path of folders are created if they are missing
extractRarArchive("/path/to/archive.rar", "~/Desktop/files");

1
非常感谢您的回答。 您的示例帮助我更好地理解了。 文档中唯一的示例是关于.extract而不是createExtractor方法。 我将尝试为想要展平提取文件(忽略目录结构)的人添加一个答案。 - saurabh

3
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);
      });

0

很遗憾,Nodejs不支持Rar压缩/解压缩,我也对此感到沮丧,因此我创建了一个名为super-winrar的模块,在nodejs中轻松处理rar文件 :)

请查看:https://github.com/KiyotakaAyanokojiDev/super-winrar

例如从“project.rar”中提取所有文件和只有package.json

const Rar = require('super-winrar');

const rar = new Rar('project.rar');
rar.on('error', error => console.log(error.message));

rar.extract({path: 'extractionDest'}, async (err) => {
  if (err) return console.log(err.message);
  console.log('extract finished!');
});

// works with async/await too;
// will extract only files into array;
const anyAsyncFunction = async () => {
  await rar.extract({path: 'extractionDest', files: ['package.json']});
};

-1
你还可以使用DecompressZip模块来解压缩zip/rar文件。有关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文件的提及。 - Whitebird
无法提取rar文件。 - lendo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接