unzip function

Future<void> unzip(
  1. List<int> bytes,
  2. String target, {
  3. bool where(
    1. String path
    ) = _anyWhere,
  4. void progress(
    1. num progress
    ) = _noProgress,
})

Unzips the raw bytes to target path with simple where filtering.

Provide a progress callback for updates between files processing.

Implementation

Future<void> unzip(
  List<int> bytes,
  String target, {
  bool Function(String path) where = _anyWhere,
  void Function(num progress) progress = _noProgress,
}) async {
  final archive = ZipDecoder().decodeBytes(bytes);
  final int files = archive.length;
  int count = 1;
  for (final file in archive) {
    final filename = file.name;
    progress(count++ / files);
    if (file.isFile && where(file.name)) {
      final fileHandle = File(path.join(target, filename));
      await fileHandle.create(recursive: true);
      await fileHandle.writeAsBytes(file.content as List<int>);
    }
  }
}