tarDirectory method

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

Implementation

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

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

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

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