lzma-web
is a JavaScript implementation of the Lempel-Ziv-Markov (LZMA) chain compression algorithm.
The codebase is a fork of LZMA-JS written by Nathan Rugg.
yarn add lzma-web
import LZMA from 'lzma-web'
const lzma = new LZMA()
const str = "Hello World"
const compressed = await lzma.compress(str)
const decompressed = await lzma.decompress(compressed)
console.log(str === decompressed) // -> true
You can also set the compression level on compress
via an optional second parameter:
// value ranges from `1` (fastest) to `9` best
const compressed = await lzma.compress("Hello World", 1)
Finally, lzma-web also supports compression progress via callbacks (for large payloads):
const str = "Hello World"
lmza.cb.compress(
str,
9, // compression level must be set when using callback
(result, error) => { // when the compression finishes or errors
if (error) throw error
console.log(results)
},
(progressPercentage) => {
console.log('the current percentage is', progressPercentage)
},
)
and decompression progress via callbacks:
const byteArray = [/* <array of bytes> */]
lzma.cb.decompress(
byteArray,
(result, error) => {
if (error) throw error
console.log(results)
},
// If the decompression progress is unable to be calculated, the
// `on_progress()` function will be triggered once with the value `-1`.
(progressPercentage) => {
console.log('the current percentage is', progressPercentage)
}
)
The repo has typescript support. You can view the types in index.d.ts
.
If the current browser supports web workers, lzma-web
uses them to prevent blocking the main thread.
Each call of new LZMA()
will create a new web worker.
In Node.js and unsupported browsers, lzma-web
is run in the main thread.
Almost all modern browsers support web workers: https://caniuse.com/webworkers
Live demos can be found at http://lzma-js.github.io/LZMA-JS/.
lzma-web
is compatible with the reference implementation of LZMA, for example, the lzma
command.