[英]Flutter:Dart unzip & save response from http
我需要這方面的幫助:使用 Flutter 1.12.13+hotfix.5 & Dart 2.7.0 我從 http.get 收到一個 zip response.bodyBytes 我如何進行 zipdecode 並直接保存到磁盤?
這是我的代碼的摘錄,以顯示我正在搜索的內容:
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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.