I am trying to create ZIP file in my flutter application using Archive package, and then want to upload newly created ZIP file to My S3 Bucket, All the operation is done successfully but when i download the file on my PC and want to extract the file it show ZIP file is corrupted. Below is the code to create and upload ZIP file:
static Future<void> zipAndUploadToBucket(FilePickerResult result) async {
Directory appDocDirectory = await getExternalStorageDirectory();
var zipFileName = DateTime.now().millisecondsSinceEpoch.toString() + '.zip';
var encoder = ZipFileEncoder();
encoder.create(appDocDirectory.path + "/" + zipFileName);
result.files.forEach((element) {
encoder.addFile(File(element.path));
});
final file = File(encoder.zip_path);
print('Final file is : ${file.lengthSync()}');
try {
final UploadFileResult result = await Amplify.Storage.uploadFile(
local: file,
key: zipFileName,
);
encoder.close();
print('Successfully uploaded file: ${result.key}');
} on StorageException catch (e) {
print('Error uploading file: $e');
}
}
Also when i try to download file in my flutter application, it throw the following exception:
Below is the code to unzip and download ZIP file from Bucket
static Future<void> unzipFile({
File zipFile,
String extractToPath,
}) async {
print('Read the Zip file from disk $zipFile');
final bytes = await zipFile.readAsBytes();
print('Bytes value is $bytes');
final archive = ZipDecoder().decodeBytes(bytes);
print('Decode the zip $archive');
print("Extract the contents of the Zip archive to extractToPath.");
for (final ArchiveFile file in archive) {
final String filename = file.name;
if (file.isFile) {
final data = file.content as List<int>;
File('$extractToPath/$filename')
..createSync(recursive: true)
..writeAsBytes(data, flush: true);
} else {
// it should be a directory
Directory('$extractToPath/$filename').create(recursive: true);
}
}
}
