tarDirectory method

Future<void> tarDirectory(
  1. Directory dir, {
  2. int compression = store,
  3. String? filename,
  4. bool followLinks = true,
  5. int? level,
  6. ZipFileProgress? filter,
})

Implementation

Future<void> tarDirectory(Directory dir,
    {int compression = store,
    String? filename,
    bool followLinks = true,
    int? level,
    ZipFileProgress? filter}) async {
  final dirPath = dir.path;
  var tarPath = filename ?? '$dirPath.tar';
  final tgzPath = filename ?? '$dirPath.tar.gz';

  Directory tempDir;
  if (compression == gzip) {
    tempDir = await Directory.systemTemp.createTemp('dart_archive');
    tarPath = '${tempDir.path}/temp.tar';
  }

  // Encode a directory from disk to disk, no memory
  open(tarPath);
  await addDirectory(Directory(dirPath),
      followLinks: followLinks, filter: filter);
  await close();

  if (compression == gzip) {
    final input = InputFileStream(tarPath);
    final output = OutputFileStream(tgzPath);
    GZipEncoder().encodeStream(input, output, level: level ?? 6);
    await input.close();
    await File(tarPath).delete();
  }
}