Downloading zip folder from server and try to extract the zip but the zip has multiple directory. I can download the zip but not able to extract it.Below code is working when zip has multiple image file but in my case has multiple directories and have multiple files.
String _localZipFileName = 'archive.zip';
Future<File> _downloadFile(String url, String fileName) async {
//url==http://115.166.142.178:3000/staff/syncdata
var req = await http.Client().get(Uri.parse(url));
Log.e("DOWNLOAD DIRECTORY",_dir);
var file = File('$_dir/$fileName');
return file.writeAsBytes(req.bodyBytes);
}
Future<void> _downloadZip(String _zipPath) async {
setState(() {
_downloading = true;
});
Log.e("Zippath",_zipPath);
_images.clear();
_tempImages.clear();
var zippedFile = await _downloadFile(_zipPath, _localZipFileName);
Log.e("Zippath",zippedFile.path);
await unarchiveAndSave(zippedFile);
setState(() {
_images.addAll(_tempImages);
_downloading = false;
});
}
unarchiveAndSave(var zippedFile) async {
var bytes = zippedFile.readAsBytesSync();
var archive = ZipDecoder().decodeBytes(bytes);
for (var file in archive) {
var fileName = '$_dir/${file.name}';
if (file.isCompressed) {
var outFile = File(fileName);
Log.e('File:: ' , outFile.path);
// _tempImages.add('${outFile.path}/productImages');
outFile = await outFile.create(recursive: true);
await outFile.writeAsBytes(file.content);
}
}
}