compress static method

Future<int> compress(
  1. String source,
  2. String archiveName
)

Compresses the contents of source into an archive named archiveName.

  • source - Source directory whose contents are archived
  • archiveName - File name of the archive, created inside source

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-Archive cmdlet
  • macOS/Linux: zip command 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;
  }
}