nodeunrar
is a npm module to extract rar archive in pure JavaScript. It's combined by a Javascript adoption layer and JavaScript unRar library compiled by Emscripten from the C++ unrar library from http://www.rarlab.com/rar_add.htm .
You can install the module via npm
:
npm install @huanjiesm/nodeunrar
createExtractorFromData(data: ArrayBuffer, password?: string)
- Create the Memory Extractor
Options:
data
: ArrayBuffer object of the RAR archive file.password
: Optional string for extraction passwordcreateExtractorFromFile(filepath: string, targetPath?: string, password?: string)
- Get the File Extractor
Options:
filepath
: File path of the RAR archive file.targetPath
: Optional string target folderpassword
: Optional string for extraction passwordgetFileList()
: Get the file list of the archive.
Return: [state, result]
{state: "FAIL", reason: <FailReason>, msg: <string>}
null
.{state: "SUCCESS"}
{
arcHeader: {
comment: "",
flags: {
authInfo: false,
headerEncrypted: false,
lock: false,
recoveryRecord: false,
solid: false,
volume: false,
},
}, fileHeaders: [
{
crc: 0,
flags: {
directory: false,
encrypted: false,
solid: false,
},
method: "Storing",
name: "FileName",
packSize: 0,
time: "2017-04-03T10:41:42.000",
unpSize: 0,
unpVer: "2.9",
},
// ...
]
}
extractAll()
: Extract all the files.
Return: [state, result]
{
arcHeader: {...} // Same as the arcHeader above
files: [
{
fileHeader: {...} // Same as the fileHeader above
extract: [state, extractedContent]
]
}
The state
above will be same as the state of the return above, and the extractedContent
will be null
if file extract failed or in File extraction mode.
If extracted successful, the extractedContent
will be Uint8Array
object.
extractFiles(files: string[], password?: string)
: Extract the specific files
Option:
files
: File path list to be extracted.password
: A different to main password may be set on these specific files.Return:
Same to the return value of extractAll()
, the result will be stored in the same order of the input array files
. If the specific file is not found in the archive, the file
item of the files
array will be null
.
var fs = require("fs");
var unrar = require("node-unrar-js");
// Read the archive file into a typedArray
var buf = Uint8Array.from(fs.readFileSync("a.rar")).buffer;
var extractor = unrar.createExtractorFromData(buf);
var list = extractor.getFileList();
if (list[0].state === "SUCCESS") {
list[1].arcHeader...
list[1].fileHeaders[...]
}
var extracted = extractor.extractAll();
var extracted = extractor.extractFiles(["1.txt", "1.txt"], "password")();
if (list[0].state === "SUCCESS") {
list[1].arcHeader...
list[1].files[0].fileHeader: ..
if (list[1].files[0].extract[0].state === "SUCCESS") {
list[1].files[0].extract[1] // Uint8Array
}
}
This module is written in TypeScript, you can import it directly in TypeScript and get the benefit of static type checking and auto-complete of IDE.
If you want to compile the module by yourself, please follow the steps below:
docker pull apiaryio/emcc
npm run prepare
npm run buildDebug
npm run buildRelease
npm run test
This module is licensed under MIT.
0.8.0:
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。