addDirectory method

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

Implementation

Future<void> addDirectory(Directory dir,
    {bool includeDirName = true,
    int? level,
    bool followLinks = true,
    void Function(double)? onProgress,
    ZipFileProgress? filter}) 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) {
    final progress = ++current / amount;
    if (filter != null) {
      final operation = filter(file, progress);
      if (operation == ZipFileOperation.cancel) {
        break;
      }
      if (operation == ZipFileOperation.skip) {
        continue;
      }
    }
    if (file is Directory) {
      var filename = path.relative(file.path, from: dir.path);
      filename = includeDirName ? '$dirName/$filename' : filename;
      final af = ArchiveFile.directory(filename);
      final stat = file.statSync();
      af.mode = stat.mode;
      af.lastModTime = stat.modified.millisecondsSinceEpoch ~/ 1000;
      _encoder.add(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(progress)));
    }
  }
  await Future.wait(futures);
}