Skip to content

Commit f85f54c

Browse files
committed
doc: Add full example for finishFlush to zlib docs
1 parent 35ca9ad commit f85f54c

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

doc/api/zlib.markdown

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,31 @@ http.createServer((request, response) => {
109109
}).listen(1337);
110110
```
111111

112+
By default, the zlib methods with throw an error when decompressing
113+
truncated data. However, if it is known that the data is incomplete, or
114+
the desire is to inspect only the beginning of a compressed file, it is
115+
possible to suppress the default error handling by changing the flushing
116+
method that is used to compressed the last chunk of input data:
117+
118+
```js
119+
// This is a truncated version of the buffer from the above examples
120+
const buffer = new Buffer('eJzT0yMA', 'base64');
121+
122+
zlib.unzip(buffer, { finishFlush: zlib.Z_SYNC_FLUSH }, (err, buffer) => {
123+
if (!err) {
124+
console.log(buffer.toString());
125+
} else {
126+
// handle error
127+
}
128+
});
129+
```
130+
131+
This will not change the behavior in other error-throwing situations, e.g.
132+
when the input data has an invalid format. Using this method, it will not be
133+
possible to determine whether the input ended prematurely or lacks the
134+
integrity checks, making it necessary to manually check that the
135+
decompressed result is valid.
136+
112137
## Memory Usage Tuning
113138

114139
<!--type=misc-->

0 commit comments

Comments
 (0)