ZetCode

Dart RawZLibFilter

last modified October 18, 2026

The RawZLibFilter class in Dart provides low-level access to zlib compression and decompression. It's part of Dart's dart:io library and handles raw zlib data streams.

With raw: true, RawZLibFilter works without zlib headers and trailers. This makes it suitable for specific protocols that use raw zlib streams.

Basic Definition

RawZLibFilter is a low-level interface to zlib. It processes data in chunks instead of converting a whole byte list at once.

Key features include configurable compression level, window bits, and memory level. Separate factories create deflate filters for compression and inflate filters for decompression.

Basic Compression

This example shows basic compression using RawZLibFilter.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() {
  var data = Uint8List.fromList('Hello, world!'.codeUnits);
  var filter = RawZLibFilter.deflateFilter(raw: true);
  
  filter.process(data, 0, data.length);
  var compressed = <int>[];
  while (true) {
    var chunk = filter.processed(end: true);
    if (chunk == null) break;
    compressed.addAll(chunk);
  }
  
  print('Compressed: $compressed');
}

We create a deflate filter with raw: true for raw compression. The process method feeds data into the filter, and processed returns the compressed bytes without zlib headers.

$ dart main.dart
Compressed: [243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 81, 4, 0]

Basic Decompression

This example demonstrates basic decompression of raw zlib data.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() {
  var compressed = Uint8List.fromList([
    243, 72, 205, 201, 201, 215, 81, 40, 207, 47, 202, 73, 81, 4, 0
  ]);
  
  var filter = RawZLibFilter.inflateFilter(raw: true);
  filter.process(compressed, 0, compressed.length);
  
  var decompressed = <int>[];
  while (true) {
    var chunk = filter.processed(end: true);
    if (chunk == null) break;
    decompressed.addAll(chunk);
  }
  
  print('Decompressed: ${String.fromCharCodes(decompressed)}');
}

We create an inflate filter with raw: true for raw decompression. The process and processed methods decompress the input data back to its original form.

$ dart main.dart
Decompressed: Hello, world!

Stream Compression

This example shows streaming compression with RawZLibFilter.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() {
  var filter = RawZLibFilter.deflateFilter(raw: true, level: 6);
  var chunks = [
    Uint8List.fromList('First part '.codeUnits),
    Uint8List.fromList('Second part '.codeUnits),
    Uint8List.fromList('Third part'.codeUnits)
  ];
  
  var compressed = <int>[];
  for (var chunk in chunks) {
    filter.process(chunk, 0, chunk.length);
    while (true) {
      var out = filter.processed(flush: false);
      if (out == null) break;
      compressed.addAll(out);
    }
  }
  
  while (true) {
    var out = filter.processed(end: true);
    if (out == null) break;
    compressed.addAll(out);
  }
  
  print('Compressed length: ${compressed.length}');
}

We process data in chunks, maintaining compression state between calls. Calling processed with end: true flushes any remaining compressed data from the filter.

$ dart main.dart
Compressed length: 26

Custom Compression Parameters

This example demonstrates custom compression parameters.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() {
  var data = Uint8List.fromList('Example data for compression'.codeUnits);
  
  var filter = RawZLibFilter.deflateFilter(
    raw: true,
    level: 9,           // Maximum compression
    windowBits: 15,     // Default window size
    memLevel: 9,        // Maximum memory usage
    strategy: ZLibOption.strategyFiltered,
  );
  
  filter.process(data, 0, data.length);
  var compressed = <int>[];
  while (true) {
    var chunk = filter.processed(end: true);
    if (chunk == null) break;
    compressed.addAll(chunk);
  }
  
  print('Compressed size: ${compressed.length} bytes');
}

We configure compression with maximum level, memory usage, and filtered strategy. These parameters affect compression ratio and performance characteristics.

$ dart main.dart
Compressed size: 30 bytes

Error Handling

This example shows error handling during decompression.

main.dart
import 'dart:io';
import 'dart:typed_data';

void main() {
  var invalidData = Uint8List.fromList([1, 2, 3, 4, 5]);
  
  try {
    var filter = RawZLibFilter.inflateFilter(raw: true);
    filter.process(invalidData, 0, invalidData.length);
    
    var result = <int>[];
    while (true) {
      var chunk = filter.processed(end: true);
      if (chunk == null) break;
      result.addAll(chunk);
    }
    print('Decompressed: $result');
  } on FormatException catch (e) {
    print('Decompression failed: ${e.message}');
  }
}

We attempt to decompress invalid data, catching the FormatException that occurs. Proper error handling is essential when working with raw compressed data.

$ dart main.dart
Decompression failed: Filter error, bad data

Best Practices

Source

Dart RawZLibFilter Documentation

This tutorial covered Dart's RawZLibFilter class with practical examples showing compression, decompression, streaming, and error handling for raw zlib data.

Author

My name is Jan Bodnar, and I am a passionate programmer with extensive programming experience. I have been writing programming articles since 2007. To date, I have authored over 1,400 articles and 8 e-books. I possess more than ten years of experience in teaching programming.

List all Dart tutorials.