use Expand-Archive and Compress-Archive in windows os utils (#58390)

Work towards removal of package:archive and ideally more stable unzipping of artifacts. These commands are available in Powershell 5+, which we already require for windows.
This commit is contained in:
Jonah Williams
2020-06-04 14:00:33 -07:00
committed by GitHub
parent eae77804fe
commit bbe18f7580
2 changed files with 152 additions and 23 deletions

View File

@@ -291,7 +291,15 @@ class _WindowsUtils extends OperatingSystemUtils {
logger: logger,
platform: platform,
processManager: processManager,
);
) {
if (processManager.canRun('pwsh.exe')) {
_activePowershell = 'pwsh.exe';
} else {
_activePowershell = 'PowerShell.exe';
}
}
String _activePowershell;
@override
void makeExecutable(File file) {}
@@ -315,36 +323,30 @@ class _WindowsUtils extends OperatingSystemUtils {
@override
void zip(Directory data, File zipFile) {
final Archive archive = Archive();
for (final FileSystemEntity entity in data.listSync(recursive: true)) {
if (entity is! File) {
continue;
}
final File file = entity as File;
final String path = file.fileSystem.path.relative(file.path, from: data.path);
final List<int> bytes = file.readAsBytesSync();
archive.addFile(ArchiveFile(path, bytes.length, bytes));
final RunResult result = _processUtils.runSync(<String>[
_activePowershell,
'-command',
'"Compress-Archive ${data.path} -DestinationPath ${zipFile.path}"',
]);
if (result.stderr.isNotEmpty) {
throw ProcessException(_activePowershell, <String>['Compress-Archive'], result.stderr);
}
zipFile.writeAsBytesSync(ZipEncoder().encode(archive), flush: true);
}
@override
void unzip(File file, Directory targetDirectory) {
final Archive archive = ZipDecoder().decodeBytes(file.readAsBytesSync());
_unpackArchive(archive, targetDirectory);
final RunResult result = _processUtils.runSync(<String>[
_activePowershell,
'-command',
'"Expand-Archive ${file.path} -DestinationPath ${targetDirectory.path}"',
], throwOnError: true);
if (result.stderr.isNotEmpty) {
throw ProcessException(_activePowershell, <String>['Expand-Archive'], result.stderr);
}
}
@override
bool verifyZip(File zipFile) {
try {
ZipDecoder().decodeBytes(zipFile.readAsBytesSync(), verify: true);
} on FileSystemException catch (_) {
return false;
} on ArchiveException catch (_) {
return false;
}
return true;
}
bool verifyZip(File zipFile) => true;
@override
void unpack(File gzippedTarFile, Directory targetDirectory) {