rar.js provides a pure javascript implementation of the rar format, allowing you to extract or manipulate packed data client-side and server-side.
Multiple inputs are supported: AJAX, File API (HTML5) and local disk (NodeJS).
rar.js makes use of dataview-extra and reader.js.
This is a very new utility/library, please see the list below for what may be missing.
TODO & Potential Features
- Large file support (currently the entire file will be in memory when
RarArchive.getis called) - Decompression support
- Encryption support
- Recognise volumes/split archives
- Parse other entries (e.g. comments)
Using rar.js is fairly straight forward.
var archive = RarArchive(file, function(err) {
if(err) {
// An error occurred (not a rar, read error, etc)
return;
}
// Use archive
});In this example, the callback is called when the archive has been opened and validated successfully. If the archive is of an invalid format or cannot be read, an appropriate error will be passed.
Within the callback, archive.entries has been populated with the files (note you may use this.entries in the callback too).
Each entry is a RarEntry instance.
By using RarArchive.get(file, callback), you can retrieve a Blob of a specified file within the archive.
What you do with this Blob is upto you. A common thing to do would be to create an object URL using URL.createObjectURL(Blob) and redirect the user to it or create an <a> element with the download attribute (HTML5) set to the file name.
When dealing with entries you have retrieved via RarArchive.get(), make sure you check the RarEntry.partial boolean.
If this boolean is true, sending/saving the Blob will result in a partial file. You must request that the user open the previous or next volume and prepend/append to the Blob to be able to retrieve the full file.
To find out if the file is continued in a previous or next volume, see RarEntry.continues and RarEntry.continuesFrom.
RarArchive(options, callback)If options is a string, it is assumed to be a URL. If it is a File instance, it will be treated as such.callbackwill be called when the archive has been validated and is ready.RarArchive.entriesAn array ofRarEntryinstances contained within this archiveRarArchive.get(RarEntry, callback)Retrieves the specifiedRarEntryand passed aBlobof it tocallback
When creating an instance of RarArchive, the data source is guessed based on data type. If it is a string, it is assumed to be a URL and will be requested over HTTP. If it is a File instance, it will be read as one.
In the case that you want to specify the type manually or want to read a local file, you must pass it in the options like so:
RarArchive({ type: RarArchive.OPEN_LOCAL, file: 'example/file.txt'}, function() { });
You may use the following constants for type:
RarArchive.OPEN_LOCALfor local files (NodeJS only)RarArchive.OPEN_URIfor HTTP URIsRarArchive.OPEN_FILEfor File instances
nameFile namepathFile path within the archive, including file namesizeSize of the unpacked filesizePackedSize of the packed filecrcCRC of the fileoffsetOffset within the archiveblockSizeSize of this entry within the archive (including headers)headerSizeSize of the header for this entryencryptedBoolean specifying if the file is password protected or notversionRAR version usedtimeDate/time string for the filemethodCompression method used, will equal one of the method constantsosOperating system used (Windows,MS-DOS,OS/2,Unix,MacorBeOS)partialBoolean specifying if the file is partial available due to split volumes. UseRarEntry.continuesandRarEntry.continuesFrom.continuesFromBoolean specifying if the file continues from a previous volumecontinuesBoolean specifying if the file continues into the next volume
The following constants also exist for use with RarEntry.method:
RarEntry.METHOD_STORERarEntry.METHOD_FASTESTRarEntry.METHOD_FASTRarEntry.METHOD_NORMALRarEntry.METHOD_GOODRarEntry.METHOD_BEST
MIT