Skip to content

landamessenger/zstandard

Repository files navigation

pub package
pub package

Zstandard

Zstandard (zstd) is a fast, high-compression algorithm developed by Meta (formerly Facebook) designed for real-time compression scenarios. It offers a flexible range of compression levels, allowing both high-speed and high-ratio compression, making it ideal for applications with diverse performance needs. Zstandard is widely used in data storage, transmission, and backup solutions.

This repository contains a federated Flutter plugin and a CLI package for zstandard compression, enabling both in-app and command-line usage. The two main components are:

  • zstandard: A Flutter plugin for cross-platform compression, supporting mobile, desktop, and web platforms. This package integrates the zstd library through FFI for native platforms and WebAssembly for the web, allowing efficient data compression in any Flutter environment.

  • zstandard_cli: A pure Dart package providing CLI capabilities for macOS, Windows, and Linux. It enables command-line compression and decompression for data files and streams.


Compatibility

Android iOS Web macOS Windows Linux Fuchsia
Flutter ✔️ ✔️ ✔️ (wasm) ✔️ ✔️ ✔️
Native FFI FFI WebAssembly FFI FFI FFI
Precompiled No No Yes No No No
macOS Windows Linux
x64 ✔️ ✔️ ✔️
arm64 ✔️ ✔️ ✔️
Precompiled Yes Yes Yes

Basic Usage

In-App (Flutter)

import 'package:zstandard/zstandard.dart';

void main() async {
  final zstandard = Zstandard();

  Uint8List originalData = Uint8List.fromList([...]);

  Uint8List? compressed = await zstandard.compress(originalData);
  
  Uint8List? decompressed = await zstandard.decompress(compressed ?? Uint8List(0));
}

Using extensions:

import 'package:zstandard/zstandard.dart';

void main() async {
  Uint8List originalData = Uint8List.fromList([...]);

  Uint8List? compressed = await originalData.compress();
  
  Uint8List? decompressed = await compressed.decompress();
}

Command Line (zstandard_cli)

# Compress a file with a specified compression level
dart run zstandard_cli:compress myfile.txt 3

# Decompress a file
dart run zstandard_cli:decompress myfile.txt.zstd

Dart Code Example (CLI)

import 'package:zstandard_cli/zstandard_cli.dart';

void main() async {
  var cli = ZstandardCLI();

  final originalData = Uint8List.fromList([...]);

  final compressed = await cli.compress(originalData, compressionLevel: 3);

  final decompressed = await cli.decompress(compressed ?? Uint8List(0));
}

Using extensions in Dart:

import 'package:zstandard_cli/zstandard_cli.dart';

void main() async {
  final originalData = Uint8List.fromList([...]);

  final compressed = await originalData.compress(compressionLevel: 3);

  final decompressed = await compressed.decompress();
}

Screenshots

Compression and Decompression Samples

macOS

Windows

Linux

Flutter Plugin (Android, iOS, macOS, Web, Windows, Linux)


License

This project uses code from the original facebook/zstd repository. Please see the LICENSE file for more information.