2
votes

I am trying to extract .rar files using node.js in windows 8.1. Is there any good way to do this?

Thanks in advance

3
I would refer to the following response stackoverflow.com/a/27450112/2824993 - cdurth
@cdurth is there any other way that doesn't use Rar, which is a closed-source software? I already tried this but another problem emerging is that: After I download a rar file using http.get, and run that code, it says that file I attempt to extract is not a rar file while it's clearly a rar file - Mr Cold

3 Answers

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

Please check unrar this may help

*This script tested on Linux Ubuntu

2
votes

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
votes

You can also use DecompressZip module for extracting zip/rar files.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";
}
});