Dart native extension for the LZ4 codec.
import "dart:typed_data";
import 'package:dart_lz4/dart_lz4.dart' as lz4;
void main() {
// Alternatively you can specify lz4.CompressionMode.HIGH for higher
// performance but less speed.
lz4.Lz4Codec codec = new lz4.Lz4Codec(mode : lz4.CompressionMode.FAST);
String test = '''Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor
incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud
exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute
irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat
nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui
officia deserunt mollit anim id est laborum.''';
// The compressor will *prepend* the original length to the compressed stream output
Uint8List compressed = codec.encode(new Uint8List.fromList(test.codeUnits));
Uint8List decompressed = codec.decode(compressed);
print( new String.fromCharCodes(decompressed) );
}
The LZ4 decompressor needs to be aware of the original uncompressed data length. To ensure that
the codec encode()
output can be piped to the codec decode()
input, the encoder prepends the length
of the uncompressed data to the compressed data stream as a 4-byte unsigned int.
To obtain access to the actual compressed data without reallocating memory you can work with views:
Uint8List codecOutput = ...
int uncompressedLen = new ByteData.view(codecOutput.buffer, 0, 4).getUInt32(0);
Uint8List compressedData = new Uint8List.view(codecOutput.buffer, 4, codecOutput.lengthInBytes - 4);
To build the extension you need to install cmake. If you have installed the
dart-sdk at a non-standard location (e.g. your home directory) you need to set the DART_SDK
env
var to point to the dart-sdk folder. The extension can be built as follows:
cmake .
make
Install Microsoft Windows SDK for Windows 7
cmake . -G"Visual Studio 10 2010 Win64"
cmake --build . --config Release
Precompiled 64-bit binaries for linux, mac and windows can be obtained by downloading the latest binary release from github or by checking out the master-binaries branch.
To fetch the master-binaries branch add the following to your pubspec.yaml
file:
dependencies:
dart_lz4:
git:
url: git://github.com/achilleasa/dart_lz4.git
ref: master-binaries
See the Contributing Guide.
dart_lz4 is distributed under the MIT license.