0
# Decompression Operations
1
2
Fast data decompression with automatic format detection for deflate and gzip formats, supporting both binary and string output. Provides both one-shot function API and streaming class API for different use cases.
3
4
## Capabilities
5
6
### Inflate Function
7
8
Decompresses deflate/gzip data with automatic format detection.
9
10
```javascript { .api }
11
/**
12
* Decompress deflate/gzip data (auto-detects format)
13
* @param input - Compressed data (Uint8Array or ArrayBuffer)
14
* @param options - Decompression options
15
* @returns Decompressed data as Uint8Array or string (if to: 'string' specified)
16
*/
17
function inflate(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
18
```
19
20
**Usage Examples:**
21
22
```javascript
23
import { inflate } from 'pako';
24
25
// Decompress to binary
26
const compressed = new Uint8Array([/* compressed data */]);
27
const decompressed = inflate(compressed);
28
29
// Decompress to string
30
const decompressedText = inflate(compressed, { to: 'string' });
31
console.log(decompressedText); // Original text
32
33
// Decompress with custom options
34
const result = inflate(compressed, {
35
windowBits: 15,
36
chunkSize: 32768
37
});
38
```
39
40
### Inflate Raw Function
41
42
Decompresses raw deflate data (no wrapper/header).
43
44
```javascript { .api }
45
/**
46
* Decompress raw deflate data (no wrapper)
47
* @param input - Compressed data (Uint8Array or ArrayBuffer)
48
* @param options - Decompression options (raw: true is automatically set)
49
* @returns Decompressed data as Uint8Array or string
50
*/
51
function inflateRaw(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
52
```
53
54
**Usage Examples:**
55
56
```javascript
57
import { inflateRaw } from 'pako';
58
59
// Decompress raw deflate data
60
const rawCompressed = new Uint8Array([/* raw deflate data */]);
61
const decompressed = inflateRaw(rawCompressed);
62
63
// To string with custom window size
64
const text = inflateRaw(rawCompressed, {
65
to: 'string',
66
windowBits: 12
67
});
68
```
69
70
### Ungzip Function
71
72
Direct alias for the inflate function - decompresses data with automatic format detection (including gzip). This function is identical to `inflate` and uses the same implementation.
73
74
```javascript { .api }
75
/**
76
* Direct alias for inflate function (auto-detects format including gzip)
77
* Note: This is literally assigned to the inflate function in the source code
78
* @param input - Compressed data (Uint8Array or ArrayBuffer)
79
* @param options - Decompression options
80
* @returns Decompressed data as Uint8Array or string
81
*/
82
function ungzip(input: Uint8Array | ArrayBuffer, options?: InflateOptions): Uint8Array | string;
83
```
84
85
**Usage Examples:**
86
87
```javascript
88
import { ungzip } from 'pako';
89
90
// Decompress gzip data
91
const gzipData = new Uint8Array([/* gzip data */]);
92
const decompressed = ungzip(gzipData, { to: 'string' });
93
```
94
95
### Inflate Class
96
97
Streaming decompression interface for processing large compressed data in chunks.
98
99
```javascript { .api }
100
/**
101
* Generic streaming wrapper for zlib inflate operations (decompression)
102
* @param options - Decompression options
103
*/
104
class Inflate {
105
constructor(options?: InflateOptions);
106
107
/** Final decompressed output (available after completion) */
108
result: Uint8Array | string;
109
110
/** Error code (0 = success) */
111
err: number;
112
113
/** Error message */
114
msg: string;
115
116
/** Internal output chunks */
117
chunks: Array<Uint8Array>;
118
119
/** Whether decompression has ended */
120
ended: boolean;
121
122
/** Configuration options */
123
options: InflateOptions;
124
125
/** Internal zlib stream structure */
126
strm: any;
127
128
/** Gzip header information (when available) */
129
header: any;
130
131
/**
132
* Process input data chunk
133
* @param data - Input compressed data chunk
134
* @param flush_mode - Flush mode (false=Z_NO_FLUSH, true=Z_FINISH, or constant)
135
* @returns true if successful, false on error
136
*/
137
push(data: Uint8Array | ArrayBuffer, flush_mode?: number | boolean): boolean;
138
139
/**
140
* Data callback (override for custom handling)
141
* @param chunk - Output data chunk
142
*/
143
onData(chunk: Uint8Array): void;
144
145
/**
146
* Completion callback
147
* @param status - Final status code
148
*/
149
onEnd(status: number): void;
150
}
151
```
152
153
**Usage Examples:**
154
155
```javascript
156
import { Inflate, constants } from 'pako';
157
158
// Basic streaming decompression
159
const inflater = new Inflate({ to: 'string' });
160
inflater.push(compressedChunk1, false);
161
inflater.push(compressedChunk2, false);
162
inflater.push(compressedChunk3, true); // true = Z_FINISH
163
console.log(inflater.result); // Decompressed string
164
165
// Custom chunk processing
166
const inflater2 = new Inflate();
167
inflater2.onData = function(chunk) {
168
console.log('Decompressed chunk:', chunk);
169
};
170
inflater2.onEnd = function(status) {
171
console.log('Decompression finished with status:', status);
172
if (this.header) {
173
console.log('Gzip header info:', this.header);
174
}
175
};
176
177
inflater2.push(gzipData, true);
178
179
// Error handling
180
const inflater3 = new Inflate();
181
inflater3.push(corruptedData, true);
182
if (inflater3.err !== constants.Z_OK) {
183
console.error('Decompression failed:', inflater3.msg);
184
}
185
```
186
187
## Options Reference
188
189
### InflateOptions
190
191
```javascript { .api }
192
interface InflateOptions {
193
/** Window size (8-15, default 15) - must match the value used during compression */
194
windowBits?: number;
195
196
/** Preset dictionary - must match the dictionary used during compression */
197
dictionary?: Uint8Array | ArrayBuffer | string;
198
199
/** Output chunk size in bytes (default 65536) */
200
chunkSize?: number;
201
202
/** Use raw inflate format (no wrapper) */
203
raw?: boolean;
204
205
/** Output format ('string' for UTF-16 string, default is Uint8Array) */
206
to?: string;
207
}
208
```
209
210
### Gzip Header Information
211
212
The `header` property on `Inflate` instances contains gzip header information when decompressing gzip data. The structure varies based on the actual gzip header content and should be accessed after decompression completes.
213
214
## Error Handling
215
216
All decompression functions and classes provide error information:
217
218
```javascript
219
import { inflate, Inflate, constants } from 'pako';
220
221
// Function API error handling
222
try {
223
const result = inflate(compressedData);
224
console.log('Decompression successful');
225
} catch (error) {
226
console.error('Decompression failed:', error.message);
227
}
228
229
// Class API error handling
230
const inflater = new Inflate();
231
inflater.push(compressedData, true);
232
233
if (inflater.err === constants.Z_OK) {
234
console.log('Success:', inflater.result);
235
} else if (inflater.err === constants.Z_DATA_ERROR) {
236
console.error('Invalid or corrupted data:', inflater.msg);
237
} else if (inflater.err === constants.Z_MEM_ERROR) {
238
console.error('Insufficient memory:', inflater.msg);
239
} else {
240
console.error('Other error:', inflater.msg);
241
}
242
```
243
244
## Working with Different Data Types
245
246
### String Output
247
248
```javascript
249
import { inflate } from 'pako';
250
251
// Automatic UTF-8 detection and conversion
252
const textData = inflate(compressedText, { to: 'string' });
253
254
// Manual string conversion for specific encodings
255
const binaryData = inflate(compressedData);
256
const customText = new TextDecoder('utf-8').decode(binaryData);
257
```
258
259
### Binary Output
260
261
```javascript
262
import { inflate } from 'pako';
263
264
// Default binary output
265
const binaryResult = inflate(compressedData); // Uint8Array
266
267
// Convert to other typed arrays if needed
268
const uint16Array = new Uint16Array(binaryResult.buffer);
269
const float32Array = new Float32Array(binaryResult.buffer);
270
```