Skip to content

Latest commit

 

History

History
89 lines (64 loc) · 2.88 KB

README-zh.md

File metadata and controls

89 lines (64 loc) · 2.88 KB

arithmetic-coding

npm JavaScript Style Guide Coveralls travis build status License

安装

安装为模块供 API 调用:

npm i arithmetic-coding

或者全局安装为命令行界面:

npm i -g arithmetic-coding

API

通过指定文件目录调用:

const ariCoding = require('arithmetic-coding');
// 从文件编码
ariCoding.encode(path.resolve('/txt/long.txt'), path.resolve('/txt/long-encoded.txt'));
// 从文件解码
ariCoding.decode(path.resolve('/txt/long-encoded.txt'), path.resolve('/txt/long-decoded.txt'));

通过传入 Buffer 调用:

let data = Buffer.from('Example data', 'utf8');
// 从 Buffer 编码
let encoded = encode.encodeFromBuffer(data);
console.log(`encoded = ${encoded}`);
// 从 Buffer 解码
let decoded = decode.decodeFromBuffer(encoded);
console.log(`decoded = ${decoded}`);

命令行界面

$ ari-coding -h
Usage: index [options] [command]

Options:
  -v, --version              output the version number
  -h, --help                 output usage information

Commands:
  encode|e [options] <file>  encode a file
  decode|d [options] <file>  decode a file

$ ari-coding encode -h
Usage: encode|e [options] <file>

encode a file

Options:
  -o, --output <file>  output file path
  -h, --help           output usage information

性能

您可以查看 travis 测试日志 得知最新版本的运行时间。

一些运行测试结果如下:

文件大小 (字节) 总运行时间 编码时间 解码时间
60640 110ms 极小 110ms
2130640 2940ms 426ms 2514ms

关于算数编码算法

  1. 维基百科
  2. 编码/解码的实际可用的实现
  3. GitHub 上 Reference-arithmetic-coding 的源代码