zipDirectory method
Zips a dir to a Zip file asynchronously.
Implementation
Future<void> zipDirectory(Directory dir,
{String? filename,
int? level,
bool followLinks = true,
void Function(double)? onProgress,
DateTime? modified,
ZipFileProgress? filter}) async {
final outputPath = _composeZipDirectoryPath(dir: dir, filename: filename);
create(outputPath, level: level ??= gzip, modified: modified);
// A callback that throws keeps a valid zip of the entries added before it,
// as cancel does. Any other failure deletes the incomplete zip, as
// TarFileEncoder does
var callbackFailed = false;
T call<T>(T Function() callback) {
try {
return callback();
} catch (_) {
callbackFailed = true;
rethrow;
}
}
try {
await addDirectory(dir,
includeDirName: false,
level: level,
followLinks: followLinks,
onProgress: onProgress == null
? null
: (value) => call(() => onProgress(value)),
filter: filter == null
? null
: (entry, value) => call(() => filter(entry, value)));
} catch (_) {
if (callbackFailed) {
try {
await close();
} catch (_) {}
} else {
try {
await _output.close();
} catch (_) {}
try {
await File(outputPath).delete();
} catch (_) {}
}
rethrow;
}
await close();
}