我正在尝试在Windows 8.1中使用node.js提取.rar文件。有没有好的方法可以做到这一点?
提前致谢。
提前致谢。
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");
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;
}
}
});
const unrarp = require('unrar-promise');
unrarp
.extractAll('rar-file-path', 'extract-directory')
.then(result => {
cb(null, result);
})
.catch(err => {
cb(err);
});
很遗憾,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']});
};
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";
}
});