GUIDES Binary Data Decompress Binary Data – Streaming
About
Binary Data
Index
Convert String to ArrayBuffer
Convert ArrayBuffer to String
Convert ArrayBuffers to String
Handle Errors Converting ArrayBuffer to String
Immutable ArrayBuffers
Resize an ArrayBuffer
Combine ArrayBuffers
Convert Base64 to Binary Data
Convert Binary Data to Base64
Convert Binary Data to Hex
Convert Hex to Binary Data
Calculate CRC for Binary Data
Compress Binary Data – One Buffer
Compress Binary Data – Streaming
Decompress Binary Data – One Buffer
Decompress Binary Data – Streaming
Callbacks
Index
One-Time Callback
Repeating Callback
Repeating Callback with Initial Delay
Immediate Callback
Reschedule Callback
Cancel Callback
Suspend Callback
What About setTimeout?
Optimizing Embedded JavaScript
Index
When to Optimize
Know Where to Optimize
Looping through an Array
Iterating Over a String
Building a String
Avoid Copying Buffers
Accessing Properties
Map versus Object
Appending to an Array
Operating on Bits
Defining Class Methods
Reducing Stack Use
Time
Index
Get Unix Time
Get Time of Day
Get Date
Get Time Since System Start
Get Microseconds
Set System Date and Time
Set Real-Time Clock Time
Get Time and Date from Network
Get Time and Date from Real-Time Clock
Sleep

Decompress Binary Data – Streaming

The Inflate module provides zlib decompression using a subset of the Pako API.

Stream multiple compressed input data buffers to Inflate by setting the second argument to push() to true on only the last buffer.

const inflator = new Inflate();

const compressedData = Uint8Array.of(
	0x78, 0xda, 0xed, 0xc5, 0xb1, 0x0d,
	0x00, 0x20, 0x08, 0x00, 0x30, 0x04,
	0x84, 0xff, 0x3f, 0xf6, 0x0e, 0x93,
	0x76, 0x69, 0x9c, 0xac, 0xbe, 0xb3,
	0x61, 0xdb, 0xb6, 0x6d, 0xdb, 0xb6,
	0x6d, 0xdb, 0xb6, 0x6d, 0xdb, 0xb6,
	0xbf, 0xfd, 0x01, 0x19, 0x00, 0x70,
	0x01);

inflator.push(compressedData.subarray(0, compressedData.length >> 1), false);
inflator.push(compressedData.subarray(compressedData.length >> 1), true);

// => inflator.result is a Uint8Array with 8192 decompressed bytes

inflator.close();

Stream the output of Inflate by implementing onData() and onEnd() callbacks. Streaming output is useful when the decompressed data is larger than available memory.

This compressedData decompresses to 8192 bytes.

const inflator = new Inflate();

inflator.onData = buffer => {
	// => buffer is Uint8Array of decompressed data
};

inflator.onEnd = error => {
	if (error) {
		// decompression failed. handle error.
	}
	else {
		// decompression successfully completed.
	}
};

const compressedData = Uint8Array.of(
	0x78, 0xda, 0xed, 0xc5, 0xb1, 0x0d,
	0x00, 0x20, 0x08, 0x00, 0x30, 0x04,
	0x84, 0xff, 0x3f, 0xf6, 0x0e, 0x93,
	0x76, 0x69, 0x9c, 0xac, 0xbe, 0xb3,
	0x61, 0xdb, 0xb6, 0x6d, 0xdb, 0xb6,
	0x6d, 0xdb, 0xb6, 0x6d, 0xdb, 0xb6,
	0xbf, 0xfd, 0x01, 0x19, 0x00, 0x70,
	0x01);
inflator.push(compressedData, true);

inflator.close();