如何在Dart中使用GZip或类似方法压缩字符串? [英] How to compress a string using GZip or similar in Dart?
本文介绍了如何在Dart中使用GZip或类似方法压缩字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在Dart中压缩字符串(在浏览器中).我试过了:
I want to compress a string in Dart (in the browser). I tried this:
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
不适用于此目的,并且不起作用(文件不可读).
Obviously UTF8.decode
is not intended for this and it doesn't work (file is unreadable).
在Dart中压缩字符串的正确方法是什么?
What is the right way to compress a string in Dart?
推荐答案
压缩的字节列表可能不是有效的UTF8序列,而是可以在base64中对其进行编码.
The compressed list of bytes is probably not a valid UTF8 sequence instead you could encode it in base64.
import 'dart:convert';
import 'package:archive/archive.dart';
void main() {
var myString = 'myString';
var stringBytes = utf8.encode(myString);
var gzipBytes = GZipEncoder().encode(stringBytes);
var compressedString = base64.encode(gzipBytes);
print(compressedString);
}
这篇关于如何在Dart中使用GZip或类似方法压缩字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文