Skip to content

Commit b5c5772

Browse files
authored
Merge pull request #8 from 43081j/typescript
1.0.0
2 parents 05f1d47 + da5b22f commit b5c5772

28 files changed

+1855
-386
lines changed

.editorconfig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
root = true
2+
3+
[*]
4+
indent_size = 2
5+
indent_style = space
6+
trim_trailing_whitespace = true

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
*.swp
2-
build/
2+
lib/
3+
node_modules/

.npmignore

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
node_modules
2+
tslint.json
3+
src/
4+
lib/test/
5+
test/
6+
.travis.yml
7+
.editorconfig

.travis.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
language: node_js
2+
node_js:
3+
- "8"
4+
- "node"
5+
script:
6+
- npm test

README.md

Lines changed: 41 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,67 @@
1-
rar.js - JavaScript Unrar Utility
2-
===
1+
# rar.js - JavaScript Unrar Utility
32

43
**rar.js** provides a pure javascript implementation of the rar format, allowing you to extract or manipulate packed data client-side and server-side.
54

65
Multiple inputs are supported: AJAX, File API (HTML5) and local disk (NodeJS).
76

8-
**rar.js** makes use of [dataview-extra](https://github.com/43081j/dataview-extra) and [reader.js](https://github.com/43081j/reader.js).
7+
## Example
98

10-
**This is a very new utility/library, please see the list below for what may be missing.**
9+
Using **rar.js** is fairly straight forward.
10+
11+
```javascript
12+
Rar.fromLocal('myfile.rar').then((archive) => {
13+
// Use archive here
14+
console.log(archive.entries);
15+
});
1116

12-
**TODO & Potential Features**
17+
Rar.fromUri('/test.rar').then((archive) => {
18+
// Use archive here
19+
});
20+
21+
Rar.fromFile(input.files[0]).then((archive) => {
22+
// Use archive here
23+
});
24+
```
25+
26+
## Unsupported features (TODO)
1327

1428
* Large file support (currently the entire file will be in memory when `RarArchive.get` is called)
1529
* Decompression support
1630
* Encryption support
1731
* Recognise volumes/split archives
1832
* Parse other entries (e.g. comments)
1933

20-
Example
21-
===
34+
## Saving files
2235

23-
Using **rar.js** is fairly straight forward.
36+
By using `RarArchive#get(file)`, you can retrieve a `Blob` of a specified file within the archive.
2437

2538
```javascript
26-
var archive = RarArchive(file, function(err) {
27-
if(err) {
28-
// An error occurred (not a rar, read error, etc)
29-
return;
30-
}
31-
// Use archive
32-
});
39+
const file = archive.get(archive.entries[0]);
40+
const url = URL.createObjectURL(file);
41+
// Do something with url here
42+
// like creating an <a> tag with the download attribute set
3343
```
3444

35-
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.
36-
37-
Within the callback, `archive.entries` has been populated with the files (note you may use `this.entries` in the callback too).
38-
39-
Each entry is a `RarEntry` instance.
40-
41-
Saving files
42-
===
43-
44-
By using `RarArchive.get(file, callback)`, you can retrieve a `Blob` of a specified file within the archive.
45-
46-
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.
47-
48-
Split Volumes
49-
===
45+
### Split Volumes
5046

5147
When dealing with entries you have retrieved via `RarArchive.get()`, make sure you check the `RarEntry.partial` boolean.
5248

5349
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.
5450

5551
To find out if the file is continued in a previous or next volume, see `RarEntry.continues` and `RarEntry.continuesFrom`.
5652

57-
RarArchive
58-
===
53+
## RarArchive
5954

60-
* `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. `callback` will be called when the archive has been validated and is ready.
61-
* `RarArchive.entries` An array of `RarEntry` instances contained within this archive
62-
* `RarArchive.get(RarEntry, callback)` Retrieves the specified `RarEntry` and passed a `Blob` of it to `callback`
63-
64-
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.
65-
66-
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:
67-
68-
```
69-
RarArchive({ type: RarArchive.OPEN_LOCAL, file: 'example/file.txt'}, function() { });
70-
```
55+
* `Rar.fromFile(file)` where `file` is a HTML5 `File` instance
56+
* `Rar.fromLocal(path)` where `path` is a string of a local filesystem path
57+
* `Rar.fromUri(uri)` where `uri` is a string of a URI
7158

72-
You may use the following constants for `type`:
59+
All three of these entrypoints return a `Promise` which resolves to a `RarArchive`.
7360

74-
* `RarArchive.OPEN_LOCAL` for local files (NodeJS only)
75-
* `RarArchive.OPEN_URI` for HTTP URIs
76-
* `RarArchive.OPEN_FILE` for File instances
61+
* `RarArchive#entries` An array of `RarEntry` instances contained within this archive
62+
* `RarArchive#get(RarEntry)` Retrieves the specified `RarEntry` resolves a promise with a `Blob`
7763

78-
RarEntry Properties
79-
===
64+
## RarEntry Properties
8065

8166
* `name` File name
8267
* `path` File path within the archive, including file name
@@ -97,14 +82,13 @@ RarEntry Properties
9782

9883
The following constants also exist for use with `RarEntry.method`:
9984

100-
* `RarEntry.METHOD_STORE`
101-
* `RarEntry.METHOD_FASTEST`
102-
* `RarEntry.METHOD_FAST`
103-
* `RarEntry.METHOD_NORMAL`
104-
* `RarEntry.METHOD_GOOD`
105-
* `RarEntry.METHOD_BEST`
85+
* `Rar.RarMethod.STORE`
86+
* `Rar.RarMethod.FASTEST`
87+
* `Rar.RarMethod.FAST`
88+
* `Rar.RarMethod.NORMAL`
89+
* `Rar.RarMethod.GOOD`
90+
* `Rar.RarMethod.BEST`
10691

107-
License
108-
===
92+
## License
10993

11094
MIT

examples/index.html

Lines changed: 25 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,28 @@
11
<!DOCTYPE html>
22
<html>
3-
<head>
4-
<title>Test</title>
5-
</head>
6-
<body>
7-
<!-- Make a file input for you to select an archive -->
8-
<input type="file">
9-
<br>
10-
<!-- Include rar.js -->
11-
<script src="../dist/rar.js"></script>
12-
<script type="text/javascript">
13-
document.querySelector('input[type="file"]').onchange = function(e) {
14-
/*
15-
Pass the File instance to rar.js
16-
You could iterate over this.files to handle many at once
17-
*/
18-
var file = RarArchive(this.files[0], function(err) {
19-
if(err) {
20-
console.log(err);
21-
return;
22-
}
23-
this.entries.forEach(function(val) {
24-
var div = document.createElement('div');
25-
div.textContent = val.path;
26-
document.body.appendChild(div);
27-
});
28-
});
29-
}
30-
</script>
31-
</body>
3+
<head>
4+
<title>Test</title>
5+
</head>
6+
<body>
7+
<!-- Make a file input for you to select an archive -->
8+
<input type="file">
9+
<br>
10+
<!-- Include rar.js -->
11+
<script src="../rar.js"></script>
12+
<script type="text/javascript">
13+
document.querySelector('input[type="file"]').onchange = function(e) {
14+
/*
15+
Pass the File instance to rar.js
16+
You could iterate over this.files to handle many at once
17+
*/
18+
var file = Rar.fromFile(this.files[0]).then((archive) => {
19+
archive.entries.forEach(function(val) {
20+
var div = document.createElement('div');
21+
div.textContent = val.path;
22+
document.body.appendChild(div);
23+
});
24+
});
25+
}
26+
</script>
27+
</body>
3228
</html>

examples/test.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
var RarArchive = require('rarjs');
1+
var Rar = require('rarjs');
22

3-
var file = RarArchive({type: RarArchive.OPEN_LOCAL, file: '../build/test.rar'}, function(err) {
4-
this.entries.forEach(function(val) {
3+
Rar.fromLocal('test.rar').then((archive) => {
4+
archive.entries.forEach((val) => {
55
console.log(val.path);
66
});
77
});

lib/dataview-extra.js

Lines changed: 0 additions & 103 deletions
This file was deleted.

0 commit comments

Comments
 (0)