'How to access a ZIP file asset directly (without storing unpacked file temporarily) in Flutter?

I want to read a zip file which is in my asset folder. I do not want to write the unpacked version to storage, but to directly process the zip file stream. The zip file only contains one file (which is retrieved by archive.first). However, I fail to convert a ByteData to an InputStream.

Is this possible?

import 'dart:convert';
import 'dart:typed_data';

import 'package:archive/archive.dart';



  @override
  void initState() {
    super.initState();

    DefaultAssetBundle.of(context)
        .load("assets/data/WZ2008.zip")
        .then((ByteData value) {
      Uint8List wzzip =
          value.buffer.asUint8List(value.offsetInBytes, value.lengthInBytes);
      final archive = ZipDecoder().decodeBuffer(???);   <- How to solve this?
      ArchiveFile a = archive.first;
      WZ2008 = jsonDecode(a.content)["Inhalt"]
          .map<String>((e) => e["Stichwort"].toString())
          .toList();
    });
}

Thanks!



Solution 1:[1]

I found a solution to this, if someone needs it:

    DefaultAssetBundle.of(context)
        .load("assets/data/WZ2008.zip")
        .then((ByteData value) {
      Uint8List wzzip =
          value.buffer.asUint8List(value.offsetInBytes, value.lengthInBytes);
      InputStream ifs = InputStream(wzzip);
      final archive = ZipDecoder().decodeBuffer(ifs);
      ArchiveFile a = archive.first;
      OutputStream os = OutputStream();
      a.decompress(os);
      String decomp = utf8.decode(os.getBytes());
      dynamic json = jsonDecode(decomp);
      WZ2008 =
          json["Inhalt"].map<String>((e) => e["Stichwort"].toString()).toList();
      a.close();
      ifs.close();
    });

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Defarine