flutter/dart: How to decompress/inflate zlib binary string in flutter
You don't show how you are receiving the data, but don't go via a string. For example, if using package:http
, use:
var bytes = response.bodyBytes;
Next, it doesn't look like you should be using gzip
, but rather zlib
;
var inflated = zlib.decode(bytes);
var data = utf8.decode(inflated);
json.decode(data);
Ramesh Guntha
Updated on December 02, 2022Comments
-
Ramesh Guntha over 1 year
I am using pako package on my nodejs server and sending compressed binary string from server to my flutter client. I am unable to decompress/inflate it on the flutter client. I have tried a combination of libraries
My Server NodeJS Code:
var pako = require('pako'); let buffer = pako.deflate(JSON.stringify(userModels), { to: 'string' }); //Server code for sending to client
My Flutter Code:
import 'dart:io'; import 'dart:convert'; List<int> gzipBytes = serverResponse.data.codeUnits; List<int> stringBytes = gzip.decode(gzipBytes); var data = utf8.decode(stringBytes);
I am getting the following exception..
Restarted application in 1,725ms. E/flutter (25340): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: InternalError: 'Filter error, bad data' E/flutter (25340): #0
_FilterImpl.processed (dart:io-patch/filter_patch.dart:11:32) E/flutter (25340): #1 _FilterSink.addSlice (dart:io/data_transformer.dart:610:29) E/flutter (25340): #2
_FilterSink.add (dart:io/data_transformer.dart:596:5) E/flutter (25340): #3 ZLibDecoder.convert (dart:io/data_transformer.dart:465:9) E/flutter (25340): #4
Codec.decode (dart:convert/codec.dart:26:34)-
Chenna Reddy over 4 yearsCan you print hexadecimals of the content being transferred (both on server and client), just to make sure you are receiving what is sent by server. Did you set
Content-Type
properly (like for exampleapplication/octet-stream
)? Also check ifContent-Encoding
can help you, then you don't need to compress at all.
-
-
Ramesh Guntha over 4 yearsI am sending the response from server as a Json object. Hence I had to convert the compressed zlib data to a binary string to make it part of the json object. Based on your suggestion, I changed the server to send compressed integers instead of strings and now it works. Thanks