lzma1 0.0.5 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +108 -30
- package/dist/index.d.ts +6 -0
- package/dist/index.js +6 -0
- package/dist/lzma.d.ts +1 -4
- package/dist/lzma.js +387 -393
- package/package.json +10 -16
package/README.md
CHANGED
|
@@ -1,38 +1,43 @@
|
|
|
1
1
|
# lzma1
|
|
2
2
|
|
|
3
|
-
This is a [fork][fork-link] of [Nathan Rugg's][fork-author] package
|
|
4
|
-
types and makes the logic more structured and readable.
|
|
3
|
+
This is a [fork][fork-link] of [Nathan Rugg's][fork-author] LZMA-JS package.
|
|
5
4
|
|
|
6
5
|
[fork-link]: https://github.com/LZMA-JS/LZMA-JS
|
|
7
6
|
[fork-author]: https://github.com/nmrugg
|
|
8
7
|
|
|
8
|
+
## Why
|
|
9
|
+
|
|
10
|
+
There are many LZMA implementations in JavaScript, but most are outdated,
|
|
11
|
+
unmaintained, lack type support, or rely on specific runtime APIs (e.g., Node.js
|
|
12
|
+
`Buffer`).
|
|
13
|
+
|
|
14
|
+
This version has been cleaned up, with TypeScript types added to make it easier
|
|
15
|
+
to read and maintain.
|
|
16
|
+
|
|
17
|
+
## Features
|
|
18
|
+
|
|
19
|
+
- Encode and decode LZMA streams
|
|
20
|
+
- Supports both string and Uint8Array data
|
|
21
|
+
- Supports both browser and Node.js environments
|
|
22
|
+
- Pure JavaScript implementation with TypeScript types
|
|
23
|
+
- No dependencies on runtime-specific APIs
|
|
24
|
+
|
|
9
25
|
## Installation
|
|
10
26
|
|
|
11
27
|
> [!NOTE]
|
|
12
|
-
> This package is native [ESM][mozzila-esm] and no longer provides a
|
|
13
|
-
>
|
|
14
|
-
>
|
|
28
|
+
> This package is native [ESM][mozzila-esm] and no longer provides a CommonJS
|
|
29
|
+
> export. If your project uses CommonJS, you will have to convert to ESM or use
|
|
30
|
+
> the dynamic [`import()`][mozzila-import] function.
|
|
15
31
|
|
|
16
32
|
[mozzila-esm]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Modules
|
|
17
33
|
[mozzila-import]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/import
|
|
18
34
|
|
|
19
|
-
### npm
|
|
35
|
+
### [npm](https://npmjs.com/lzma1)
|
|
20
36
|
|
|
21
37
|
```sh
|
|
22
38
|
npm install lzma1
|
|
23
39
|
```
|
|
24
40
|
|
|
25
|
-
### deno
|
|
26
|
-
|
|
27
|
-
Since `v1.28+` import from npm registry using `npm:` prefix.
|
|
28
|
-
|
|
29
|
-
```ts
|
|
30
|
-
import {
|
|
31
|
-
compress,
|
|
32
|
-
decompress,
|
|
33
|
-
} from "npm:lzma1@latest";
|
|
34
|
-
```
|
|
35
|
-
|
|
36
41
|
### browser
|
|
37
42
|
|
|
38
43
|
```html
|
|
@@ -41,16 +46,16 @@ import {
|
|
|
41
46
|
</script>
|
|
42
47
|
```
|
|
43
48
|
|
|
44
|
-
##
|
|
49
|
+
## Quick start
|
|
50
|
+
|
|
51
|
+
The library provides two main functions: `compress` and `decompress`:
|
|
45
52
|
|
|
46
53
|
```ts
|
|
47
54
|
compress(data: string | Uint8Array, mode?: Mode): Int8Array
|
|
48
55
|
decompress(data: Uint8Array | ArrayBuffer): string | Int8Array
|
|
49
56
|
```
|
|
50
57
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
Compress and decompress a string with compression level 1.
|
|
58
|
+
### Compressing a string
|
|
54
59
|
|
|
55
60
|
```js
|
|
56
61
|
import {
|
|
@@ -59,22 +64,95 @@ import {
|
|
|
59
64
|
} from "lzma1";
|
|
60
65
|
|
|
61
66
|
const data = "Hello World!";
|
|
62
|
-
const compressed = compress(data, 1);
|
|
63
|
-
const decompressed = decompress(
|
|
67
|
+
const compressed = compress(data, 1); // Using compression level 1 (fastest)
|
|
68
|
+
const decompressed = decompress(compressed);
|
|
64
69
|
|
|
65
70
|
// data === decompressed
|
|
66
71
|
```
|
|
67
72
|
|
|
68
|
-
|
|
73
|
+
### Working with binary data
|
|
69
74
|
|
|
70
|
-
|
|
75
|
+
```js
|
|
76
|
+
import {
|
|
77
|
+
compress,
|
|
78
|
+
decompress,
|
|
79
|
+
} from "lzma1";
|
|
71
80
|
|
|
72
|
-
|
|
81
|
+
// Compress binary data
|
|
82
|
+
const binaryData = new Uint8Array([0x01, 0x02, 0x03, 0x04]);
|
|
83
|
+
const compressed = compress(binaryData, 5); // Default compression level
|
|
73
84
|
|
|
74
|
-
|
|
85
|
+
// Decompress back to binary
|
|
86
|
+
const decompressed = decompress(compressed);
|
|
87
|
+
// decompressed will be an Int8Array
|
|
88
|
+
```
|
|
75
89
|
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
90
|
+
### HTML example
|
|
91
|
+
|
|
92
|
+
A simple browser example:
|
|
93
|
+
|
|
94
|
+
```html
|
|
95
|
+
<!DOCTYPE html>
|
|
96
|
+
<html>
|
|
97
|
+
<body>
|
|
98
|
+
<textarea id="input">Hello World!</textarea><br>
|
|
99
|
+
<button id="run">Compress & Decompress</button>>
|
|
100
|
+
<div id="result"></div>
|
|
101
|
+
|
|
102
|
+
<script type="module">
|
|
103
|
+
import { compress, decompress } from "https://esm.sh/lzma1@latest";
|
|
104
|
+
|
|
105
|
+
document.getElementById("run").onclick = () => {
|
|
106
|
+
const text = document.getElementById("input").value;
|
|
107
|
+
const compressed = compress(text);
|
|
108
|
+
const decompressed = decompress(compressed);
|
|
109
|
+
|
|
110
|
+
document.getElementById("result").innerHTML =
|
|
111
|
+
`Original: ${text.length} bytes<br>` +
|
|
112
|
+
`Compressed: ${compressed.length} bytes<br>` +
|
|
113
|
+
`Result: ${decompressed}`;
|
|
114
|
+
};
|
|
115
|
+
</script>
|
|
116
|
+
</body>
|
|
117
|
+
</html>
|
|
118
|
+
```
|
|
119
|
+
|
|
120
|
+
### Advanced usage
|
|
121
|
+
|
|
122
|
+
You can control the compression level (1-9) to balance between speed and compression ratio:
|
|
123
|
+
|
|
124
|
+
```js
|
|
125
|
+
import { compress } from "lzma1";
|
|
126
|
+
|
|
127
|
+
// Fastest compression (level 1)
|
|
128
|
+
const fastCompressed = compress(data, 1);
|
|
129
|
+
|
|
130
|
+
// Balanced compression (level 5)
|
|
131
|
+
const balancedCompressed = compress(data, 5);
|
|
132
|
+
|
|
133
|
+
// Maximum compression (level 9)
|
|
134
|
+
const maxCompressed = compress(data, 9);
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
## How it works
|
|
138
|
+
|
|
139
|
+
LZMA (Lempel-Ziv-Markov chain Algorithm) is a compression algorithm that uses a
|
|
140
|
+
dictionary compression scheme. It's known for its high compression ratio and is
|
|
141
|
+
commonly used in the 7z archive format.
|
|
142
|
+
|
|
143
|
+
### LZMA header
|
|
144
|
+
|
|
145
|
+
The LZMA compressed data begins with a header that contains information needed for decompression:
|
|
146
|
+
|
|
147
|
+

|
|
148
|
+
|
|
149
|
+
More [information][header_link] about the LZMA header structure.
|
|
79
150
|
|
|
80
151
|
[header_link]: https://docs.fileformat.com/compression/lzma/#lzma-header
|
|
152
|
+
|
|
153
|
+
## Related
|
|
154
|
+
|
|
155
|
+
- [7-Zip SDK](https://www.7-zip.org/sdk.html)
|
|
156
|
+
- [lzma-purejs](https://github.com/cscott/lzma-purejs)
|
|
157
|
+
- [lzmajs](https://github.com/glinscott/lzmajs)
|
|
158
|
+
- [lzma-purejs fork](https://github.com/mauron85/lzma-purejs/tree/master)
|
package/dist/index.d.ts
ADDED
package/dist/index.js
ADDED
package/dist/lzma.d.ts
CHANGED
|
@@ -47,7 +47,6 @@ export declare class LZMA {
|
|
|
47
47
|
readonly modeIndex: 1;
|
|
48
48
|
};
|
|
49
49
|
};
|
|
50
|
-
readonly bitMaskForRange = -16777216;
|
|
51
50
|
constructor();
|
|
52
51
|
GetLenToPosState(len: number): number;
|
|
53
52
|
StateUpdateChar(index: number): number;
|
|
@@ -63,7 +62,6 @@ export declare class LZMA {
|
|
|
63
62
|
compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof typeof this.CompressionModes): Int8Array;
|
|
64
63
|
decompress(bytearray: Uint8Array | ArrayBuffer): Int8Array | string;
|
|
65
64
|
}
|
|
66
|
-
type CompressionMode = keyof LZMA["CompressionModes"];
|
|
67
65
|
/**
|
|
68
66
|
* Compresses data using LZMA algorithm
|
|
69
67
|
*
|
|
@@ -71,7 +69,7 @@ type CompressionMode = keyof LZMA["CompressionModes"];
|
|
|
71
69
|
* @param mode Compression mode (1-9), defaults to 5
|
|
72
70
|
* @returns Compressed data as Int8Array
|
|
73
71
|
*/
|
|
74
|
-
export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?:
|
|
72
|
+
export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?: keyof LZMA["CompressionModes"]): Int8Array;
|
|
75
73
|
/**
|
|
76
74
|
* Decompresses LZMA compressed data
|
|
77
75
|
*
|
|
@@ -79,4 +77,3 @@ export declare function compress(data: string | Uint8Array | ArrayBuffer, mode?:
|
|
|
79
77
|
* @returns Decompressed data as string if input was string, or Int8Array if input was binary
|
|
80
78
|
*/
|
|
81
79
|
export declare function decompress(data: Uint8Array | ArrayBuffer): string | Int8Array;
|
|
82
|
-
export {};
|