addDirectory method

Future<void> addDirectory(
  1. Directory dir, {
  2. bool includeDirName = true,
  3. int? level,
  4. bool followLinks = true,
  5. void onProgress(
    1. double
    )?,
})

Implementation

Future<void> addDirectory(
  Directory dir, {
  bool includeDirName = true,
  int? level,
  bool followLinks = true,
  void Function(double)? onProgress,
}) async {
  final dirName = path.basename(dir.path);
  final files = dir.listSync(recursive: true, followLinks: followLinks);
  final futures = <Future<void>>[];
  final amount = files.length;
  var current = 0;
  for (final file in files) {
    if (file is Directory) {
      var filename = path.relative(file.path, from: dir.path);
      filename = includeDirName ? '$dirName/$filename' : filename;
      final af = ArchiveFile('$filename/', 0, null);
      af.mode = file.statSync().mode;
      af.lastModTime =
          file.statSync().modified.millisecondsSinceEpoch ~/ 1000;
      af.isFile = false;
      _encoder.addFile(af);
    } else if (file is File) {
      final dirName = path.basename(dir.path);
      final relPath = path.relative(file.path, from: dir.path);
      futures.add(
          addFile(file, includeDirName ? '$dirName/$relPath' : relPath, level)
              .then((value) => onProgress?.call(++current / amount)));
    }
  }
  await Future.wait(futures);
}