Emscripten unrar lib for extract rar archive.
TypeScript
96
357 commits
updated Jun 19, 2024
node-unrar.js 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 which hosted on http://www.rarlab.com/rar_add.htm .
You can install the module via npm:
npm install node-unrar-js
async function createExtractorFromData(options: ExtractorFromDataOptions): Promise<Extractor<Uint8Array>> - Create the in Memory Extractor
Options ExtractorFromDataOptions:
data: ArrayBuffer : ArrayBuffer object of the RAR archive filepassword?: string : Optional passwordwasmBinary? ArrayBuffer; : Optional Use in browser/webpack, the wasm binary must be loaded in the code and send to this function to load the wasm codeasync function createExtractorFromFile(options: ExtractorFromFileOptions): Promise<Extractor> - Get the File Extractor
Options ExtractorFromFileOptions:
filepath: string : File path of the RAR archive filetargetPath?: string : Optional target folderpassword?: string : Optional passwordfilenameTransform?: (filename: string) => string: Optional transform the file name before it's created on file systemwasmBinary? ArrayBuffer; : Optional Use in nodejs/webpack, the wasm binary must be loaded in the code and send to this function to load the wasm code in webpack based nodejs project (please read Used in Webpack-bundled NodeJS Project for more details).Node: This function is not available in EM2015 Module since the EM2015 Module is used for webpack in Browser.
getFileList(): ArcList : Get the file header and file list of the archive.
Members in ArcList:
arcHeader: ArcHeader : The header of the archivefileHeaders: Generator<FileHeader> : The iterator of the FileHeader objects{
arcHeader: {
comment: "",
flags: {
authInfo: false,
headerEncrypted: false,
lock: false,
recoveryRecord: false,
solid: false,
volume: false,
},
}, fileHeaders: (Iterator)
{
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",
comment: "",
},
}
extract(options: ExtractOptions): Extract the files.
ExtractOptions:
files?: string[] | ((fileHeader: FileHeader) => boolean) Optional Extract all the files if files is empty
string[]: Extract the specific files only(fileHeader: FileHeader) => boolean: Extract only the filtered file (eg. extract only the files without password).password?: string: Optional password for the extracted files only (Different password can be applied to any single file in RAR archive).createExtractorFromData and createExtractorFromFile:
ArcFiles<Uint8Array> for createExtractorFromDataArcFiles for createExtractorFromFileArcFiles:
arcHeader: ArcHeader : The header of the archivefiles: Generator<ArcFile> : The iterator of the ArcFile objectsArcFile:
fileHeader: FileHeader : The header of the extracted fileextraction?: Uint8Array : The extracted content of the file (createExtractorFromData only). If the ArcFile is a folder (ArcFile.fileHeader.flags.directory is true), the extraction will be undefined, otherwise it will be the content of the file (in `Uint8Array).{
arcHeader: {...} // Same as the arcHeader above
files: (Iterator)
{
fileHeader: {...} // Same as the fileHeader above
extraction?: Uint8Array // createExtractorFromData only
]
}
Note: The returned iterators from the two apis are lazy, it means the file header/content will not be parsed/extracted if any file is not iterated yet.
The customized Error Object UnrarError will be thrown for any exception in extracting. The definition of the Object is:
class UnrarError extends Error {
reason: FailReason;
file?: string | undefined; // Will be filled for any exception in extraction of a file
}
The following code is used in the FailReason:
| FailReason | Message |
|---|---|
| ERAR_NO_MEMORY | Not enough memory |
| ERAR_BAD_DATA | Archive header or data are damaged |
| ERAR_BAD_ARCHIVE | File is not RAR archive |
| ERAR_UNKNOWN_FORMAT | Unknown archive format |
| ERAR_EOPEN | File open error |
| ERAR_ECREATE | File create error |
| ERAR_ECLOSE | File close error |
| ERAR_EREAD | File read error |
| ERAR_EWRITE | File write error |
| ERAR_SMALL_BUF | Buffer for archive comment is too small, comment truncated |
| ERAR_UNKNOWN | Unknown error |
| ERAR_MISSING_PASSWORD | Password for encrypted file or header is not specified |
| ERAR_EREFERENCE | Cannot open file source for reference record |
| ERAR_BAD_PASSWORD | Wrong password is specified |
Note: although the return value fileHeaders and files are iterators, they must be traversed to the end! Otherwise the C++ object for archive extraction will not be destructed and cause memory leak.
const fs = require("fs");
const unrar = require("node-unrar-js");
async function main() {
// Read the archive file into a typedArray
const buf = Uint8Array.from(fs.readFileSync("a.rar")).buffer;
const extractor = await unrar.createExtractorFromData({ data: buf });
const list = extractor.getFileList();
const listArcHeader = list.arcHeader; // archive header
const fileHeaders = [...list.fileHeaders]; // load the file headers
const extracted = extractor.extract({ files: ["1.txt"] });
// extracted.arcHeader : archive header
const files = [...extracted.files]; //load the files
files[0].fileHeader; // file header
files[0].extraction; // Uint8Array content, createExtractorFromData only
}
main();
This package can also be used in browser by Webpack, please visit the demo project to see how to use it in webpack.
In most cases the exported ESModule is used in browser by Webpack, but in case if the NodeJs project (or an Electron project) is bundled by Webpack, the wasmBinary data must be loaded manually just like the browser by Webpack, it can be loaded like this:
import fs from 'fs';
import { createExtractorFromFile } from 'node-unrar-js/esm';
const wasmBinary = fs.readFileSync(require.resolve('node-unrar-js/esm/js/unrar.wasm'));
const extractor = await createExtractorFromFile({ wasmBinary, filepath: './WithComment.rar' });
// const list = extractor.getFileList();
Note: the package must be loaded from 'node-unrar-js/esm' instead of 'node-unrar-js' to enable the function createExtractorFromFile in ES Module.
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:
Install the docker
Download the c++ source of unRar library by:
npm run downloadUnrarSrc
npm run build:debug
npm run build:release
npm run test
This module is licensed under MIT.
Overall
Maintained
Code review
Latest release
Releases this year
96 followers · starred Nov 2024
Emscripten unrar lib for extract rar archive.
TypeScript
96
357 commits
updated Jun 19, 2024
node-unrar.js 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 which hosted on http://www.rarlab.com/rar_add.htm .
You can install the module via npm:
npm install node-unrar-js
async function createExtractorFromData(options: ExtractorFromDataOptions): Promise<Extractor<Uint8Array>> - Create the in Memory Extractor
Options ExtractorFromDataOptions:
data: ArrayBuffer : ArrayBuffer object of the RAR archive filepassword?: string : Optional passwordwasmBinary? ArrayBuffer; : Optional Use in browser/webpack, the wasm binary must be loaded in the code and send to this function to load the wasm codeasync function createExtractorFromFile(options: ExtractorFromFileOptions): Promise<Extractor> - Get the File Extractor
Options ExtractorFromFileOptions:
filepath: string : File path of the RAR archive filetargetPath?: string : Optional target folderpassword?: string : Optional passwordfilenameTransform?: (filename: string) => string: Optional transform the file name before it's created on file systemwasmBinary? ArrayBuffer; : Optional Use in nodejs/webpack, the wasm binary must be loaded in the code and send to this function to load the wasm code in webpack based nodejs project (please read Used in Webpack-bundled NodeJS Project for more details).Node: This function is not available in EM2015 Module since the EM2015 Module is used for webpack in Browser.
getFileList(): ArcList : Get the file header and file list of the archive.
Members in ArcList:
arcHeader: ArcHeader : The header of the archivefileHeaders: Generator<FileHeader> : The iterator of the FileHeader objects{
arcHeader: {
comment: "",
flags: {
authInfo: false,
headerEncrypted: false,
lock: false,
recoveryRecord: false,
solid: false,
volume: false,
},
}, fileHeaders: (Iterator)
{
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",
comment: "",
},
}
extract(options: ExtractOptions): Extract the files.
ExtractOptions:
files?: string[] | ((fileHeader: FileHeader) => boolean) Optional Extract all the files if files is empty
string[]: Extract the specific files only(fileHeader: FileHeader) => boolean: Extract only the filtered file (eg. extract only the files without password).password?: string: Optional password for the extracted files only (Different password can be applied to any single file in RAR archive).createExtractorFromData and createExtractorFromFile:
ArcFiles<Uint8Array> for createExtractorFromDataArcFiles for createExtractorFromFileArcFiles:
arcHeader: ArcHeader : The header of the archivefiles: Generator<ArcFile> : The iterator of the ArcFile objectsArcFile:
fileHeader: FileHeader : The header of the extracted fileextraction?: Uint8Array : The extracted content of the file (createExtractorFromData only). If the ArcFile is a folder (ArcFile.fileHeader.flags.directory is true), the extraction will be undefined, otherwise it will be the content of the file (in `Uint8Array).{
arcHeader: {...} // Same as the arcHeader above
files: (Iterator)
{
fileHeader: {...} // Same as the fileHeader above
extraction?: Uint8Array // createExtractorFromData only
]
}
Note: The returned iterators from the two apis are lazy, it means the file header/content will not be parsed/extracted if any file is not iterated yet.
The customized Error Object UnrarError will be thrown for any exception in extracting. The definition of the Object is:
class UnrarError extends Error {
reason: FailReason;
file?: string | undefined; // Will be filled for any exception in extraction of a file
}
The following code is used in the FailReason:
| FailReason | Message |
|---|---|
| ERAR_NO_MEMORY | Not enough memory |
| ERAR_BAD_DATA | Archive header or data are damaged |
| ERAR_BAD_ARCHIVE | File is not RAR archive |
| ERAR_UNKNOWN_FORMAT | Unknown archive format |
| ERAR_EOPEN | File open error |
| ERAR_ECREATE | File create error |
| ERAR_ECLOSE | File close error |
| ERAR_EREAD | File read error |
| ERAR_EWRITE | File write error |
| ERAR_SMALL_BUF | Buffer for archive comment is too small, comment truncated |
| ERAR_UNKNOWN | Unknown error |
| ERAR_MISSING_PASSWORD | Password for encrypted file or header is not specified |
| ERAR_EREFERENCE | Cannot open file source for reference record |
| ERAR_BAD_PASSWORD | Wrong password is specified |
Note: although the return value fileHeaders and files are iterators, they must be traversed to the end! Otherwise the C++ object for archive extraction will not be destructed and cause memory leak.
const fs = require("fs");
const unrar = require("node-unrar-js");
async function main() {
// Read the archive file into a typedArray
const buf = Uint8Array.from(fs.readFileSync("a.rar")).buffer;
const extractor = await unrar.createExtractorFromData({ data: buf });
const list = extractor.getFileList();
const listArcHeader = list.arcHeader; // archive header
const fileHeaders = [...list.fileHeaders]; // load the file headers
const extracted = extractor.extract({ files: ["1.txt"] });
// extracted.arcHeader : archive header
const files = [...extracted.files]; //load the files
files[0].fileHeader; // file header
files[0].extraction; // Uint8Array content, createExtractorFromData only
}
main();
This package can also be used in browser by Webpack, please visit the demo project to see how to use it in webpack.
In most cases the exported ESModule is used in browser by Webpack, but in case if the NodeJs project (or an Electron project) is bundled by Webpack, the wasmBinary data must be loaded manually just like the browser by Webpack, it can be loaded like this:
import fs from 'fs';
import { createExtractorFromFile } from 'node-unrar-js/esm';
const wasmBinary = fs.readFileSync(require.resolve('node-unrar-js/esm/js/unrar.wasm'));
const extractor = await createExtractorFromFile({ wasmBinary, filepath: './WithComment.rar' });
// const list = extractor.getFileList();
Note: the package must be loaded from 'node-unrar-js/esm' instead of 'node-unrar-js' to enable the function createExtractorFromFile in ES Module.
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:
Install the docker
Download the c++ source of unRar library by:
npm run downloadUnrarSrc
npm run build:debug
npm run build:release
npm run test
This module is licensed under MIT.
Overall
Maintained
Code review
Latest release
Releases this year
96 followers · starred Nov 2024