我需要帮助:
使用Flutter 1.12.13 + hotfix.5和Dart 2.7.0
我从http.get收到一个zip response.bodyBytes
如何压缩并直接保存到磁盘?

这是我的代码的一部分,以显示我在搜索什么:

import 'dart:async';
import 'dart:io';
...
  var basicAuth = 'my auth';
  Map<String, String> headers = new HashMap();
  headers.putIfAbsent('authorization', () => basicAuth);
  headers.putIfAbsent('Content-Type', () => 'application/json');

  var url = 'http://myhost';

  http.Response response = await http.get(
      url,
      headers: headers,
  );

  var dir = await getApplicationDocumentsDirectory();
  String path = dir.path + '/';
  String fn = 'filename.xxx';
  new File(path + fn).writeAsBytes(response.bodyBytes); // This cmd store zip file to disk
  // In my case i've a zip file with only one file inside, below a code to unzip all files inside zip
  // Extract the contents of the Zip archive to disk.
  for (ArchiveFile file in archive) {
    String filename = file.name;
    if (file.isFile) {
      List<int> data = file.content;
      File('out/' + filename)
        ..createSync(recursive: true)
        ..writeAsBytesSync(data);
    } else {
      Directory('out/' + filename)
        ..create(recursive: true);
    }
  }
  // Instead i'd like to unzip the only one file there's inside

最佳答案

如果您的数据为gzip格式,则可以尝试

GZipCodec().decode(response.bodyBytes);

关于flutter - Flutter:Dart解压缩并保存来自http的响应,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/59491294/

10-12 04:28
查看更多