From aed222ac920f8f9a1c97dc7322cbd699cd374711 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 25 Dec 2024 16:43:19 +0100 Subject: [PATCH] Revert "Combine tars and encode using archive lib" This reverts commit 5b4ec65ba982177bcae0426ed68e8b8dc2b5df3c. --- lib/api/archive.dart | 50 ++++++++++++++++++++++--------------- lib/api/docker_images.dart | 7 +++--- test/dockerimages_test.dart | 2 +- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/lib/api/archive.dart b/lib/api/archive.dart index 75816ad..b52fe6a 100644 --- a/lib/api/archive.dart +++ b/lib/api/archive.dart @@ -1,24 +1,33 @@ import 'dart:io'; -import 'package:archive/archive_io.dart'; - -/// API for archive operations +/// API for 7-Zip archive operations class ArchiveApi { + static const _exe = './7zip/7za.exe'; + + /// Get current path + static Future get currentPath async { + // Get current path + try { + final result = await Process.run("cmd", ["/c", "cd"], runInShell: true); + return result.stdout.toString(); + } catch (e) { + throw Exception('Failed to get current path: $e'); + } + } + /// Extracts the archive at [archivePath] to [destinationPath] static Future extract( String archivePath, String destinationPath) async { + // 7zr.exe x layer2.tar.gz -olayer // Extract archive - final inputStream = InputFileStream(archivePath); - final extracted = GZipDecoder().decodeBuffer(inputStream); - // Write to destination - final outputFile = File(destinationPath); - outputFile.create(recursive: true); - await outputFile.writeAsBytes(extracted); - inputStream.close(); + try { + await Process.run(_exe, ['x', archivePath, '-o$destinationPath']); + } catch (e) { + throw Exception('Failed to extract archive: $e'); + } } - /// Merge the tar archives at [archivePaths] into [destinationPath] - /// Trailing zeros are removed from the files. + /// Merge the archives at [archivePaths] into [destinationPath] static Future merge( List archivePaths, String destinationPath) async { // Merge archives @@ -56,13 +65,14 @@ class ArchiveApi { } /// Compress the tar archive at [filePath] to [destinationPath] - static void compress(String filePath, String destinationPath) { - // compress tar to gzip - final inputFileStream = InputFileStream(filePath); - final outputFileStream = OutputFileStream(destinationPath); - GZipEncoder().encode(inputFileStream, - output: outputFileStream, level: Deflate.BEST_SPEED); - inputFileStream.close(); - outputFileStream.close(); + static Future compress(String filePath, String destinationPath) async { + // 7zr.exe a -tgzip full_image.tar.gz full_image.tar + // Compress tar archive + try { + // 7zr.exe a -ttar combined_image.tar merged\* + await Process.run(_exe, ['a', '-tgzip', destinationPath, filePath]); + } catch (e) { + throw Exception('Failed to compress tar archive: $e'); + } } } diff --git a/lib/api/docker_images.dart b/lib/api/docker_images.dart index 172d73a..22f7a36 100644 --- a/lib/api/docker_images.dart +++ b/lib/api/docker_images.dart @@ -450,14 +450,13 @@ class DockerImage { // Extract layer final layerTarGz = parentPath.file('layer_$i.tar.gz'); - final layerTar = parentPath.file('layer_$i.tar'); - await ArchiveApi.extract(layerTarGz, layerTar); - paths.add(layerTar); + await ArchiveApi.extract(layerTarGz, parentPath.path); + paths.add(parentPath.file('layer_$i.tar')); } // Archive as tar then gzip to disk await ArchiveApi.merge(paths, outTar); - ArchiveApi.compress(outTar, outTarGz); + await ArchiveApi.compress(outTar, outTarGz); Notify.message('writingtodisk-text'.i18n()); } else if (layers == 1) { diff --git a/test/dockerimages_test.dart b/test/dockerimages_test.dart index 0cf3cc3..c483b79 100644 --- a/test/dockerimages_test.dart +++ b/test/dockerimages_test.dart @@ -100,7 +100,7 @@ void main() { // Delete the instance await WSLApi().remove('test'); - }, timeout: const Timeout(Duration(minutes: 2))); + }); test('Create instance test nginx (nonroot)', () async { TextEditingController nameController = TextEditingController(text: 'test');