Combine tars and encode using archive lib
This commit is contained in:
@@ -1,33 +1,24 @@
|
||||
import 'dart:io';
|
||||
|
||||
/// API for 7-Zip archive operations
|
||||
import 'package:archive/archive_io.dart';
|
||||
|
||||
/// API for archive operations
|
||||
class ArchiveApi {
|
||||
static const _exe = './7zip/7za.exe';
|
||||
|
||||
/// Get current path
|
||||
static Future<String> 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<void> extract(
|
||||
String archivePath, String destinationPath) async {
|
||||
// 7zr.exe x layer2.tar.gz -olayer
|
||||
// Extract archive
|
||||
try {
|
||||
await Process.run(_exe, ['x', archivePath, '-o$destinationPath']);
|
||||
} catch (e) {
|
||||
throw Exception('Failed to extract archive: $e');
|
||||
}
|
||||
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();
|
||||
}
|
||||
|
||||
/// Merge the archives at [archivePaths] into [destinationPath]
|
||||
/// Merge the tar archives at [archivePaths] into [destinationPath]
|
||||
/// Trailing zeros are removed from the files.
|
||||
static Future<void> merge(
|
||||
List<String> archivePaths, String destinationPath) async {
|
||||
// Merge archives
|
||||
@@ -65,14 +56,13 @@ class ArchiveApi {
|
||||
}
|
||||
|
||||
/// Compress the tar archive at [filePath] to [destinationPath]
|
||||
static Future<void> 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');
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -450,13 +450,14 @@ class DockerImage {
|
||||
|
||||
// Extract layer
|
||||
final layerTarGz = parentPath.file('layer_$i.tar.gz');
|
||||
await ArchiveApi.extract(layerTarGz, parentPath.path);
|
||||
paths.add(parentPath.file('layer_$i.tar'));
|
||||
final layerTar = parentPath.file('layer_$i.tar');
|
||||
await ArchiveApi.extract(layerTarGz, layerTar);
|
||||
paths.add(layerTar);
|
||||
}
|
||||
|
||||
// Archive as tar then gzip to disk
|
||||
await ArchiveApi.merge(paths, outTar);
|
||||
await ArchiveApi.compress(outTar, outTarGz);
|
||||
ArchiveApi.compress(outTar, outTarGz);
|
||||
|
||||
Notify.message('writingtodisk-text'.i18n());
|
||||
} else if (layers == 1) {
|
||||
|
||||
@@ -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');
|
||||
|
||||
Reference in New Issue
Block a user