Skip to content
/ lzma-web Public

A JavaScript implementation of the Lempel-Ziv-Markov (LZMA) chain compression algorithm

License

Notifications You must be signed in to change notification settings

biw/lzma-web

Repository files navigation

lzma-web

Build Status version bundlephobia MIT License

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.

Install

yarn add lzma-web

Usage

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)
  }
)

Typescript

The repo has typescript support. You can view the types in index.d.ts.

Web Workers

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

Demos

Live demos can be found at http://lzma-js.github.io/LZMA-JS/.

Compatibility

lzma-web is compatible with the reference implementation of LZMA, for example, the lzma command.

License

MIT