compress static method
Compresses the contents of source into an archive named archiveName.
source- Source directory whose contents are archivedarchiveName- File name of the archive, created insidesource
Returns the process exit code (0 indicates success), or 127 when the
archiver is not installed.
The method uses platform-specific compression tools:
- Windows: PowerShell's
Compress-Archivecmdlet - macOS/Linux:
zipcommand with recursive option
Throws UnsupportedError for unsupported platforms.
Example usage:
final exitCode = await CompressFiles.compress(
'/path/to/source/directory',
'debug_symbols.zip',
);
Implementation
static Future<int> compress(String source, String archiveName) async {
final List<String> executable;
if (Platform.isWindows) {
executable = [
"powershell",
"Compress-Archive",
"-Path",
"*", // Compress all files in the working directory
"-DestinationPath",
archiveName,
"-Force",
];
} else if (Platform.isMacOS || Platform.isLinux) {
executable = ["zip", "-r", archiveName, "."];
} else {
throw UnsupportedError("Unsupported platform for compression");
}
try {
final result = await Process.run(
executable.first,
executable.sublist(1),
runInShell: Platform.isWindows,
workingDirectory: source,
);
return result.exitCode;
} on ProcessException {
return 127;
}
}