How to decrypt a json response with gzip encoded data in flutter?

Viewed 1019

The Json response has encrypted objects which has to be decrypted to get the actual process. In android GZip was used .How can I achieve this The sample Json is as mentioned below.Any help is really appreciated.


            {
                "Data": "1.´ABCD´1150275,11028´01-Jan-2021´8,000.00´",
                "Data": [
                    {
                      "Element": "8iMAAB+LCAAAAAAABADt1T8zBxwHgkefKcGh98Zcdz8FSqj9DMzK4d+L0Nj1tveNR2w6M8rRs3PJWBFDy"
                    },
                    {
                     "Element": "B1AV4bGp6JzQJI8ChnxzixrlT8vKnYHPwRM8zykKVn2gkceAFdxMwU0to"
                    }
                ],

    "Status": 1,
    "Msg": "Success",
    "APIVersion": "1.4"
}

Basically how to decrypt a Gzip string. The same process was done in android ,but im new to flutter Android java code is attached. i want to achieve something like that in flutter

    public static String decompress(String zipText) throws IOException {
        byte[] compressed = Base64.decode(zipText, Base64.DEFAULT);
        if (compressed.length > 4) {
            GZIPInputStream gzipInputStream = new GZIPInputStream(new ByteArrayInputStream(compressed, 4,compressed.length - 4));
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            for (int value = 0; value != -1; ) {
                value = gzipInputStream.read();
                if (value != -1) {
                    baos.write(value);
                }
            }
            gzipInputStream.close();
            baos.close();
            return new String(baos.toByteArray(), StandardCharsets.UTF_8);
        } else {
            return "";
        }
    }

On way i tried was

  List<int> data  = utf8.encode(zipText);
  var deCompressedString = GZipDecoder().decodeBytes(data);
  print(deCompressedString);

Which throw exception

Unhandled Exception: FormatException: Invalid GZip Signature
1 Answers

For decrypt zipped: How to decode a Gzip Http Response in Flutter?

EDIT

decompress like this

 String decompress(String zipText) {
  final List<int> compressed = base64Decode(zipText);
  if (compressed.length > 4) {
   Uint8List uint8list = GZipDecoder().decodeBytes(compressed.sublist(4, compressed.length - 4));
   // print( String.fromCharCodes(uint8list));
   return String.fromCharCodes(uint8list);
  } else {
   return "";
  }
 }
Related