Mauro Miotello
Mauro Miotello

Reputation: 21

Flutter:Dart unzip & save response from http

i need help for this: Using Flutter 1.12.13+hotfix.5 & Dart 2.7.0 i receive a zip response.bodyBytes from an http.get how can i zipdecode and save directly to disk ?

This an extract of my code to show what i'm searching:

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

Upvotes: 1

Views: 2852

Answers (1)

Frank Treacy
Frank Treacy

Reputation: 3716

IF your data is in gzip format you can try

GZipCodec().decode(response.bodyBytes);

Upvotes: 1

Related Questions