addDirectory method

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

Implementation

Future<void> addDirectory(Directory dir,
    {bool followLinks = true,
    bool includeDirName = true,
    ZipFileProgress? filter}) async {
  final files = dir.listSync(recursive: true, followLinks: followLinks);

  final dirName = path.basename(dir.path);
  final futures = <Future<void>>[];
  final numFiles = files.length;
  var fileCount = 0;
  for (final file in files) {
    final progress = ++fileCount / numFiles;
    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/');
      af.mode = (await file.stat()).mode;
      _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));
    }
  }

  await Future.wait(futures);
}