如何在 Dart 中使用 GZip 或类似工具压缩字符串?

2023-11-26

我想在 Dart 中压缩一个字符串(在浏览器中)。我试过这个:

import 'package:archive/archive.dart';

[...]

List<int> stringBytes = UTF8.encode(myString);
List<int> gzipBytes = new GZipEncoder().encode(stringBytes);
String compressedString = UTF8.decode(gzipBytes, allowMalformed: true);

明显地UTF8.decode不适用于此目的并且不起作用(文件不可读)。

在 Dart 中压缩字符串的正确方法是什么?


如果需要,您可以使用下面的这个功能。

import 'dart:convert';
import 'dart:io';

  void _compress(String json) {
    final enCodedJson = utf8.encode(json);
    final gZipJson = gzip.encode(enCodedJson);
    final base64Json = base64.encode(gZipJson);

    final decodeBase64Json = base64.decode(base64Json);
    final decodegZipJson = gzip.decode(decodeBase64Json);
    final originalJson = utf8.decode(decodegZipJson);
    }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何在 Dart 中使用 GZip 或类似工具压缩字符串? 的相关文章

随机推荐