@01/as-lzma 中文文档教程

发布于 5年前 浏览 19 项目主页 更新于 3年前

LZMA Decoder (AssemblyScript)

这是一个用 AssemblyScript

Example

示例代码

var memory = new WebAssembly.Memory({ initial: 160 })
...
// You know how to instantiate WASM module
var lzma = module.instance.exports

// Get compressed data by fetch or by some other means
var inputData = new Uint8Array(await (await fetch('PATH/TO/COMPRESSED_FILE.lzma')).arrayBuffer())

// Allocate memory to copy input data. 
const inputDataPtr = lzma.newU8Array(inputData.length)

// AssemblyScript ArrayBuffer header length
const AS_ARRAY_OFFSET = 24

// Create an Uint8Array using allocated buffer and set input data
const u8Array = new Uint8Array(
  memory.buffer,
  inputDataPtr + AS_ARRAY_OFFSET,
  inputData.length
)
u8Array.set(inputData)

class DecodeResult {
  constructor(ptr, memory) {
    const result = new Uint32Array(memory.buffer, ptr, 4)
    const [success, error, unpackSize, dataPtr] = result;
    this.success = success
    this.error = error
    if (this.success) {
      this.unpackSize = unpackSize
      this.data = new Uint8Array(
        memory.buffer,
        dataPtr + AS_ARRAY_OFFSET,
        unpackSize
      )
    }
  }
}

// Decode LZMA data
const resultPtr = lzma.decode(inputDataPtr)
const result = new DecodeResult(resultPtr, memory)

// Decompressed data
console.log(result.data)

// If the data is plain text, decode using TextDecoder
// const decoder = new TextDecoder()
// const text = decoder.decode(result.data)
// console.log(text)

How to make lzma file

编写的实验性 lzma 解码器下载 lzma sdk 并使用以下

lzma e input_filename.extension output_filename.lzma

Nidin Vinayakan< 开发的 命令/a>

License

麻省理工学院

LZMA Decoder (AssemblyScript)

This is an experimental lzma decoder written in AssemblyScript

Example

example code

var memory = new WebAssembly.Memory({ initial: 160 })
...
// You know how to instantiate WASM module
var lzma = module.instance.exports

// Get compressed data by fetch or by some other means
var inputData = new Uint8Array(await (await fetch('PATH/TO/COMPRESSED_FILE.lzma')).arrayBuffer())

// Allocate memory to copy input data. 
const inputDataPtr = lzma.newU8Array(inputData.length)

// AssemblyScript ArrayBuffer header length
const AS_ARRAY_OFFSET = 24

// Create an Uint8Array using allocated buffer and set input data
const u8Array = new Uint8Array(
  memory.buffer,
  inputDataPtr + AS_ARRAY_OFFSET,
  inputData.length
)
u8Array.set(inputData)

class DecodeResult {
  constructor(ptr, memory) {
    const result = new Uint32Array(memory.buffer, ptr, 4)
    const [success, error, unpackSize, dataPtr] = result;
    this.success = success
    this.error = error
    if (this.success) {
      this.unpackSize = unpackSize
      this.data = new Uint8Array(
        memory.buffer,
        dataPtr + AS_ARRAY_OFFSET,
        unpackSize
      )
    }
  }
}

// Decode LZMA data
const resultPtr = lzma.decode(inputDataPtr)
const result = new DecodeResult(resultPtr, memory)

// Decompressed data
console.log(result.data)

// If the data is plain text, decode using TextDecoder
// const decoder = new TextDecoder()
// const text = decoder.decode(result.data)
// console.log(text)

How to make lzma file

Download lzma sdk and use following command

lzma e input_filename.extension output_filename.lzma

Developed by Nidin Vinayakan

License

MIT

    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文