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

问题描述 投票:0回答:5

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

提前致谢

node.js module extract rar
5个回答
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 上测试


3
投票

node-unrar-js 是基于原始源代码的 JavaScript(确切地说是 TS)中的 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");

2
投票

压缩包

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";
}
});
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.