Sample code for 30+ languages & platforms
Dart

Compress Bytes to Base64 (or any other encoding)

See more Compression Examples

Compresses bytes to base64 or any other encoding. Also decompress to return the original.

Chilkat Dart Downloads

Dart
import 'dart:typed_data';

import 'package:chilkat/chilkat.dart';

void main() {
  // This example assumes the Chilkat API to have been previously unlocked.
  // See Global Unlock Sample for sample code.

  // First create some binary data to compress.
  final binData = CkBinData();

  for (var i = 1; i <= 16; i++) {
    binData.appendEncoded('000102030405060708090A0B0C0D0E0F', 'hex');
  }

  final compress = CkCompression();
  compress.algorithm = 'deflate';
  compress.encodingMode = 'base64';

  var uncompressedBytes = Uint8List(0);
  uncompressedBytes = binData.getBinary();

  // Compress and return the compressed bytes in base64 format.
  final compressedBase64 = compress.compressBytesENC(uncompressedBytes);
  print('compressed and base64 encoded: $compressedBase64');

  // Compress and return in hex format:
  compress.encodingMode = 'hex';
  final compressedHex = compress.compressBytesENC(uncompressedBytes);
  print('compressed and hex encoded: $compressedHex');

  // Now decompress..
  final binData2 = CkBinData();

  // Decompress the base64..
  compress.encodingMode = 'base64';
  uncompressedBytes = compress.decompressBytesENC(compressedBase64);
  binData2.appendBinary(uncompressedBytes);
  // Show the uncompressed bytes in hex format:
  print(binData2.getEncoded('hex'));
  print('--');

  // Decompress the hex..
  compress.encodingMode = 'hex';
  uncompressedBytes = compress.decompressBytesENC(compressedHex);
  binData2.clear();
  binData2.appendBinary(uncompressedBytes);
  // Show the uncompressed bytes in hex format:
  print(binData2.getEncoded('hex'));
  print('--');
}