我已经下载了Dart archive包,但文档有点空。我有一个对象,我需要以文件格式序列化,并压缩拉链。
这是我到目前为止所写的内容,但它不起作用。
static Future<List<int>> convertMeetingsListToZip(List<Meeting> list) async {
return File('meetings.zip')
.writeAsString(jsonEncode(list))
.then((File encodedFile) {
Archive archive = new Archive();
archive.addFile(new ArchiveFile(
encodedFile.path, encodedFile.lengthSync(), encodedFile));
return ZipEncoder().encode(archive);
});
}
你能帮帮我吗?
没关系,我做到了。这是如何做:
static List<int> convertListToZip(List<dynamic> list) {
String jsonEncoded = jsonEncode(list);
List<int> utf8encoded = utf8.encode(jsonEncoded);
ArchiveFile jsonFile =
new ArchiveFile("filename.json", utf8encoded.length, utf8encoded);
Archive zipArchive = new Archive();
zipArchive.addFile(jsonFile);
List<int> zipInBytes = new ZipEncoder().encode(zipArchive);
return zipInBytes;
}
要点是在将文件包装到存档中并对其进行编码之前,以字节(使用utf8.encode
)对文件进行编码。