ZetCode

Dart ZLibDecoder

last modified October 18, 2026

The ZLibDecoder class in Dart provides functionality to decompress data compressed with the zlib format. It's part of Dart's dart:io library.

ZLibDecoder handles the decompression of zlib-encoded data streams, commonly used in network protocols and file compression. It supports both raw and wrapped zlib streams.

Basic Definition

ZLibDecoder is a converter that transforms zlib-compressed byte data into its original uncompressed form. It is a Converter and can also be used for stream processing.

Key features include support for streaming decompression, memory efficiency, and handling of both zlib-wrapped and raw compressed data formats.

Basic ZLibDecoder Usage

This example shows basic decompression of a zlib-compressed byte list.

main.dart
import 'dart:io';

void main() {
  // Compressed data (zlib format)
  final compressed = [120, 156, 243, 72, 205, 201, 201, 7, 0, 5, 140, 1, 245];
  
  final decoder = ZLibDecoder();
  final decompressed = decoder.convert(compressed);
  
  print('Decompressed: ${String.fromCharCodes(decompressed)}');
}

We create a ZLibDecoder instance and use its convert method to decompress the byte list. The compressed data represents the string "Hello" in zlib format.

$ dart main.dart
Decompressed: Hello

Streaming Decompression

This example demonstrates streaming decompression of zlib data.

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

Future<void> main() async {
  // Simulate streaming compressed data
  final stream = Stream.fromIterable([
    [120, 156],
    [243, 72, 205, 201],
    [201, 7, 0, 5, 140, 1, 245]
  ]);
  
  final decoder = ZLibDecoder();
  final decompressed = await decoder.bind(stream).toList();
  
  final result = decompressed.expand((x) => x).toList();
  print('Decompressed: ${String.fromCharCodes(result)}');
}

We use the bind method to connect a stream of compressed data chunks to the decoder. This is memory-efficient for large compressed data streams.

$ dart main.dart
Decompressed: Hello

Handling Raw ZLib Data

This example shows how to decompress raw zlib data without headers.

main.dart
import 'dart:io';

void main() {
  // Raw compressed data (no zlib headers)
  final rawCompressed = [243, 72, 205, 201, 201, 7, 0];
  
  final decoder = ZLibDecoder(raw: true);
  final decompressed = decoder.convert(rawCompressed);
  
  print('Decompressed: ${String.fromCharCodes(decompressed)}');
}

By setting the raw parameter to true, we tell the decoder to expect raw compressed data without zlib headers. This is useful for certain protocols.

$ dart main.dart
Decompressed: Hello

Error Handling

This example demonstrates proper error handling during decompression.

main.dart
import 'dart:io';

void main() {
  // Invalid compressed data
  final invalidData = [1, 2, 3, 4, 5];
  
  final decoder = ZLibDecoder();
  
  try {
    final decompressed = decoder.convert(invalidData);
    print('Decompressed: $decompressed');
  } on FormatException catch (e) {
    print('Decompression failed: ${e.message}');
  }
}

We wrap the decompression in a try-catch block to handle potential FormatExceptions that occur with invalid compressed data.

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

Decompressing Large Files

This example shows how to decompress large zlib-compressed files efficiently.

main.dart
import 'dart:io';

Future<void> main() async {
  final inputFile = File('compressed.zlib');
  final outputFile = File('decompressed.txt');
  
  final decoder = ZLibDecoder();
  
  await outputFile.openWrite().addStream(
    inputFile.openRead().transform(decoder)
  );
  
  print('File decompressed successfully');
}

We use file streams and the transform method to decompress large files without loading the entire content into memory. This is memory-efficient for big files.

$ dart main.dart
File decompressed successfully

Best Practices

Source

Dart ZLibDecoder Documentation

This tutorial covered Dart's ZLibDecoder class with practical examples showing basic decompression, streaming, error handling, and file operations.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Dart tutorials.