如何从Dart中的Json创建zip文件?

问题描述 投票:0回答:1

我已经下载了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);
    });
  }

你能帮帮我吗?

dart archive
1个回答
1
投票

没关系,我做到了。这是如何做:

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)对文件进行编码。

© www.soinside.com 2019 - 2024. All rights reserved.