从服务器下载zip文件并尝试提取zip文件,但该zip文件具有多个目录。我可以下载zip文件,但无法提取它。以下代码适用于当zip文件具有多个图像文件时的情况,但在我的情况下,该zip文件具有多个目录和多个文件。
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);
}
}
}
ArchiveFile
给出父级压缩文件的文件名。 - Farhana Naaz Ansari