unarchive method

Stream<Es> unarchive(
  1. Directory toDir, [
  2. bool useRelPath = true,
  3. bool? isDirWritable
])

find archive in source unarchive to target directory.

  • toDir: unarchive to target directory.
  • useRelPath: use relative path If true else use deroot absolute path.
final source = '~/Downloads/ft.tgz';
final action = BasicPathAction(source);

final toDir = Direcotry(expandTilde('~/Downloads/temp/ft/'))..createSync(recursive: true);
action.unarchive(toDir);

Implementation

Stream<Es> unarchive(Directory toDir,
    [bool useRelPath = true, bool? isDirWritable]) {
  final action = PathAction.unarchive.name, chk = 'validator';
  argErr ??= validator();
  if (argErr!.isNotEmpty) throw ArgumentError.value(argErr, action, chk);
  final srcPath = (type == FileSystemEntityType.directory)
      ? Directory(path).absolute.path
      : path;

  final fields = fieldsFromOptions(fmtFields);
  final fseType = FileSystemEntityType.file;
  final stream = scEntity.stream;
  final fStream =
      stream.where((event) => event.fs.type == fseType).asBroadcastStream();
  // final fStream = stream.where((event) => isAllowArchiveType(event.$1.path));

  final dst = toDir.path;
  late StreamSubscription subs;
  subs = fStream.listen(
    (event) {
      if (!isAllowArchiveType(event.fse.path)) return; // same continue

      final (entity, stat, extra) = event.asRecord;
      final oldPath = entity.isAbsolute ? entity.path : entity.absolute.path;
      // print(srcFile);
      // keep the original, useRelPath
      var newPath = p.join(dst, pathderoot(oldPath)); // default false
      if (useRelPath) {
        if (type == FileSystemEntityType.directory) {
          newPath = p.join(dst, oldPath.substring(srcPath.length + 1));
        } else {
          newPath = p.join(dst, p.basename(oldPath));
        }
      }
      var ok = false;
      var file = File(entity.path);
      try {
        unArchive(file, toDir, fields,
            useRelPath: useRelPath,
            logger: logger,
            isDirWritable: isDirWritable);
        ok = true;
      } catch (e, s) {
        logger.stderr('e, $action, data, unArchive, $oldPath -> $newPath');
        scEntity.addError(e, s);
      }

      final line =
          Formatter(file, stat, extra, action, shows: fields, ok: ok);
      logger.stdout(line.toString());
    },
    cancelOnError: cancelOnError,
    onDone: () => logger.trace('d, $action, done.'),
    onError: (e, s) {
      if (cancelOnError) {
        exitCode = ExitCodeExt.error.code;
        subs.cancel();
      }

      logger
        ..trace('d, $action, cancelOnError:$cancelOnError')
        ..stderr('e, $action, error. $e')
        ..stderr(kIsDebug ? '$s' : '');
    }, // onError
  );

  return fStream;
}